Java-compare the size of the same object

Source: Internet
Author: User

There are two methods for determining the same in java:

First, use = to directly determine the same, which is suitable for comparison between two variables, one variable and one object.

For example:

int a=3,c=3;Integer b=new Integer(3);System.out.println(a==c);System.out.println(a==b);
Output result:

True

True

Method 2: Use the equals () function to compare two objects, one object, and one variable.

For example:

int a=3;Integer b=new Integer(3);Integer c=new Integer("3");System.out.println(b.equals(a));System.out.println(b.equals(c));
Output result:

True

True

As described above, we can only determine whether two quantities are equal. Next we will introduce a function to determine the size of two objects:

It is conpareTo ();

The specific usage is included in the following code:

// Compare public class Main_3 {public static void main (String [] args) {int a = 3, B = 4; System. out. println (a> B); System. out. println (a = B); System. out. println (
 
  
<= Integer x = new Integer (4); Integer y = new Integer (6); System. out. println (x> y); System. out. println (x = y); // Why is it not true? System. out. println (x
  
   
<== Comparison // If You Want To determine whether two objects are equal, you can use the equals () function System. out. println (x. equals (y); // if you compare the sizes of two objects, you can use the compareTo function. // the return value of this function is the int type value System. out. println (x. compareTo (y); // returns 0 to indicate that the x object is equal to the y object, and returns-1 to indicate that x is less than y, returns 1, indicating that x is greater than y. // compare the object with the basic type. System. out. println (x = B); System. out. println (x. equals (B ));}}
  
 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.