The difference between = = and the Equals method and the application of Integer and string __java

Source: Internet
Author: User
the difference between = = and the Equals method and the application of Integer and string

= = compares the address memory addresses of two objects, while equals compares the values of two objects.
For example, the Equals comparison method of string, first call = = to determine whether the same, and then determine whether the object value is the same char array. integer and int = = Operations

the integer and int = = are judged primarily by the address. Example below

int i = m;
Integer i1 = m; 
System.out.println (I==I1);//true
Integer i2 = m;
System.out.println (I1==I2);//true
integer i3 = new Integer (m)
; System.out.println (I==I3);//true
System.out.println (i1==i3);//false      
Integer i4 =;
Integer i5 =;
System.out.println (I4==I5);//false
integer I6 = new Integer (a);
System.out.println (I4==I6);//false
int i6=300;
System.out.println (I5==I6);///true

Reason: 1 automatic boxing and integer object cache array: int i = 100, involving automatic boxing, that is, integer i = integer.valueof (100), and the other integer has a cached array, that is, -128<=value<= 127, the integer object is fetched from the cached array. So I==i1,i1==i2 is true, but if the object generated by the new integer method is false, because new is meant to be a new object, so I3==i1 is false, and I==i3 is because When int is compared to an integer, the integer is split, comparing the size of value, so the same.
2 I4==i6 is the object of new because it is an integer object and value exceeds the maximum cache value, so the integer object is compared to false
3 I5==i6 Because i5 is int,i6, it is not a comparison of memory addresses, but a comparison of values.
4 So if you judge whether the size of the two integer objects is the same, use Integer.intvalue () to determine if an integer and int are judged by size, and you do not need to use Integer.intvalue (). Because an integer compared to an int is automatically disassembled, or directly using the Equals method of integer string = = and equals

String str = "abc";
String str1 = "abc";
System.out.println (STR==STR1);//true
String str2 = new String ("abc");
System.out.println (STR==STR2);//false
String str3 = new String ("abc");
System.out.println (STR2==STR3);//false

Because string also has a string constant pool, both are fetched directly from the constant pool when passed through STR==STR1. But in the new way, the object is created and stored, regardless of whether the value is in the string constant pool.

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.