Size comparison of integer and integer data

Source: Internet
Author: User


Public classtest{ Public Static voidMain (string[] args) {Integer i1= 100; Integer i2= 100;if(I1 = =I2) {System.out.println ("I1==i2");}Else{System.out.println ("I1! = i2");}}} Public classtest1{ Public Static voidMain (string[] args) {Integer i1= 200; Integer i2= 200;if(I1 = =I2) {System.out.println ("I1==i2");}Else{System.out.println ("I1! = i2");}}}

The test found that the first data output I1==I2, the second data output i1!=i2.
The reason is because
When assigning a value to an integer, it is actually an auto-boxing process, which is called the integer.valueof (int) method, which uses a constant pool when the value is greater than or equal to 128 and less than or equal to 127, so the first two addresses are equal, but the last two are more than 127, Therefore, a constant pool is not used.

Other words
Integer-128~127 actually you can think of as an int, so the output of the first class should be = =
Interger more than 128 of the value can not be regarded as int, he is the object, two different values of the same object if it is determined to be unequal, can be judged by equals.

Size comparison of integer and integer data

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.