The difference between an integer and an int

Source: Internet
Author: User

Description: The difference between int and integer:

For them, we may just know the simple difference. Integer is an encapsulated class of int, the initial value of int is 0, and the initial value of the integer is null. But are there really only these differences between them? I think the answer is no, so I decided to go deep into the JDK source code to explore. See what the difference is between an integer and an int.

Execute code:

1  Public classIntegertest {2 3      Public Static voidMain (string[] args) {4         //TODO auto-generated Method Stub5         intIntnum = 127;6Integer Integernum = 127;7Integer Integernewnum =NewInteger (127);8         9         //comparison of two int variablesTen         intINTNUM1 = 127; OneSystem.out.print ("int and int:"); ASystem.out.println (intnum==intNum1); -                  -          the         //Compare int to integer difference -System.out.print ("int and integer:"); -System.out.println (intnum==integernum); -          +         //Compare int to integer difference -System.out.print ("Int and Newinteger:"); +System.out.println (intnum==integernewnum); A          at         //compare integers with Newinteger -System.out.print ("Integer and Newinteger:"); -System.out.println (integernum==integernewnum); -                  -         //comparison of two Newinteger -Integer INTEGERNEWNUM1 =NewInteger (127); inSystem.out.print ("Newinteger and Newinteger:"); -System.out.println (integernewnum==integerNewNum1); to                  +         //compare two declaration variables less than 128 -Integer INTEGERNUM1 = 127; theSystem.out.print ("Integer and Integer less than 128:"); *System.out.println (integernum==integerNum1); $         Panax Notoginseng         //compare two declaration variables greater than or equal to 128 -Integer integerNum2 = 128; theInteger integerNum3 = 128; +System.out.print ("Integer and integer greater than or equal to 128:"); ASystem.out.println (integernum2==integerNum3); the          +     } -  $}

Operation Result:

int and integer:trueint with Newinteger:trueinteger and Newinteger:false  int and int:trueNewinteger and Newinteger:false is less than 128 integer and integer:  True integer and integer greater than or equal to 128:false

Problem:

1. Why is it true when we compare Integernum, Integernewnum, and intnum with equal numbers?

2. Why is the result false when we compare the integernum with the integernewnum with the same value?

3. Why does the Integernum compare with Integernum when there are more than 128 and less than or equal to 128 different results?

My understanding:

The premise of all the discussion is that the values of the two int variables are the same, and the result of the comparison is true, which is the result of 12 rows.

The result of 1.17 rows and 21 rows is true, but we will know the essence if we understand it from the source. We should first add a gratitude before this, and the Integer integernum =127 will be translated when executed

Integer integernum = integer.valueof (127). The source code is as follows

1  Public StaticInteger valueOf (String Arg)throwsNumberFormatException {2         returnValueOf (parseint (ARG, 10));3     }4 5      Public StaticInteger ValueOf (intArg) {6         returnArg >= -128 && arg <= Integer.IntegerCache.high?Integer.integercache.cache[arg + +]7:NewInteger (ARG);8}
1 Private Static classIntegercache {2         Static Final intLow =-128;3         Static Final intHigh ;4         Static Finalinteger[] Cache;5 6         Static {7             intarg = 127;8String arg0 = Vm.getsavedproperty ("Java.lang.Integer.IntegerCache.high");9             intarg1;Ten             if(arg0! =NULL) { One                 Try { AArg1 =Integer.parseint (arg0); -Arg1 = Math.max (arg1, 127); -arg = Math.min (arg1, 2147483518); the}Catch(NumberFormatException arg3) { -                     ; -                 } -             } +  -High =Arg; +             cache = New Integer[high- -128 + 1]; AArg1 =-128; at  -              for(intarg2 = 0; Arg2 < Cache.length; ++arg2) { -CACHE[ARG2] =NewInteger (arg1++); -             } -  -             assertHigh >= 127; in  -         } to}
1 private final int value; 2  Public Integer (int  arg0) {3         this.value = arg0; 4     }56public     throws  numberformatexception {  7this         . Value = parseint (arg0, ten); 8     }

The above JDK source code can be obtained, whether the Integer declaration or new object, the final value is the int type, so we wait for question 1 to answer. The result of comparison of two int is necessarily ture.

2.25 rows The result is false, why? Don't all turn into int? Shouldn't the result be false? The amount of the volume. New has a different address. So when we compare using "= =", the memory addresses are already different, although the values are the same. This involves the difference between "= =" and ". Equals" in the next blog post.

3. Why is there a problem 3 in this case, 128 would be a 0-point? In fact, the answer in the source code we can also find

 Public Static throws numberformatexception {2         return valueOf (parseint (ARG.)); 3     }4 5 public     static Integer valueOf (int  arg) {6        return arg >= -128 && arg <= Integer.IntegerCache.high? Integer.integercache.cache[arg + 128]7                 : New Integer (ARG); 8     }

When ARG is greater than or equal to-128 and less than or equal to 127, an already existing object is returned directly from the cache. If the value of the parameter is not within this range, then a new integer object is returned.

The above is the difference between int and integer, we are often unsure whether to use int or integer in daily project or practice, but you should have your own answer to this blog post!

The difference between an integer and an int

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.