Integer type values are equal or unequal for analysis, and integer values are not equal

Source: Internet
Author: User

Integer type values are equal or unequal for analysis, and integer values are not equal

I saw an interview question written by a blogger in the blog. One of the questions is Integer a = 1; Integer B = 1; (a = B )? True: false; at the time, I saw that this was not obvious true. I only knew it when I saw the comment and discussed it. There is a range for the Integer value comparison. Equals is usually used for comparison and judgment, which is simple and easy to use. I did not notice these details. This is a good opportunity for Google to back up data.

Use the following code for testing:

 

 1      @Test 2     public void testInteger() { 3         Integer a = -129; 4         Integer a1 = -129; 5         Integer aaa = new Integer(-129); 6  7         Integer aa = -128; 8         Integer aa1 = -128; 9 10         System.out.println("a==a1:" + (a == a1) + "--aa==aa1:" + (aa == aa1)); //   a==a1:false--aa==aa1:true11         System.out.println("aaa==a1:" + (aaa == a1));        // aaa==a1:false12         System.out.println("a.equals(a1):" + a.equals(a1));   //  a.equals(a1):true13 14         Integer b = 128;15         Integer b1 = 128;16         System.out.println("b==b1:" + (b == b1));    // b==b1:false17         System.out.println("b.equals(b1):" + b.equals(b1));  //  b.equals(b1):true18 19         Integer c = 127;20         Integer cc = 127;21         Integer d = 1;22         Integer dd = 1;23 24         System.out.println("c==cc:" + (c == cc) + "----d==dd:" + (d == dd));  // c==cc:true----d==dd:true25         System.out.println("------------");26 27         Integer e = 128;28         int e1 = 128;29         System.out.println("e == e1:" + (e == e1));  // e == e1:true30     }

It is concluded that during the period of [-128,127], the Integer can use "=.Why is this happening? In fact, when we use Integer a = number to assign values, the Integer class calls the public static Integer valueOf (int I) method.

 

 

Let's take a look at the ValueOf (int I) code. We can see that it makes an if judgment on the input parameter I. In the case of-128 <= I <= 127, the int original data type is used directly, and an object is new when the range is exceeded. We know that the "=" symbol is the memory address of the comparison object, while the original data type is the direct comparison data value. Then this problem is solved.

Note that Integer e = 128; int e1 = 128; e = e1: true and Integer B = 128; Integer b1 = 128; B = b1: false, e = 128 is already greater than 127, so e is an object (new). Why e = e1 is true, because int is a value type, comparing the reference type Integer With the value type int, it obviously compares the value because int does not open up memory in the heap, the value in the stack is his own value, so e = e1 compares their respective values, and e = e1 is true.

Conclusion: During the period of [-128,127], the value of the Integer type can be equal to "=". The value of the Integer type is compared with that of the int type (=.

 

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.