Summarize the differences between integer and int in Java for detailed parsing

Source: Internet
Author: User
One of the previous articles mentions why Java generics use objects instead of primitive types. But the difference between the integer and int is still not understand, continue to Baidu a bit, found a big guy's article, Feel good, just reprint share.

The difference between an integer and an int

Integer is the encapsulated class provided by int, and int is the basic data type of Java; the integer default value is null, and the int default value is 0, the variable declared as an integer needs to be instantiated, and the variable declared as int does not need to be instantiated; An integer is an object. Point to this object with a reference, and int is the basic type that stores the value directly.

In addition to knowing the most basic differences between integers and int, you need to know a little bit about the problems encountered in the actual use of integers and int.
Otherwise, if the interviewer asks again, integer i = 1;int II = 1; is I==ii true or false? It is estimated that some people can not answer the question, if you ask others, it is estimated that more people will have a confused mind. So I summed them up and I hope to be helpful to everyone.
The code is as follows:

public class Main {public static void main (string[] args) {int a = 128;        Integer B = 127;        Integer C = 127;        Integer d = 128;        Integer e = 128;        Integer f = new Integer ("128");        Integer g = new Integer ("127");          Integer h = new Integer ("127");        Scenario I () System.out.println ("Scenario One: The comparison of the values of the different initialization methods of the integer and the new integer, the integer is not equal to the new integer");        Integer e = 128;        Integer f = new Integer ("128");        if (E = = f) {System.out.println ("true");        }else{System.out.println ("false");         } System.out.println ("---------------------");        Scenario Two System.out.println ("scenario two: Create two integers that are all directly assigned");        Integer B = 127;        Integer C = 127;        if (b = = c) {System.out.println ("true");        }else{System.out.println ("false");        }//integer d = 128;        Integer e = 128;  if (d = = e) {System.out.println ("true");      }else{System.out.println ("false");         } System.out.println ("---------------------");        Programme three System.out.println ("scheme three: two are all new out of the integer");        Integer g = new Integer ("127");        Integer h = new Integer ("127");        if (g = = h) {System.out.println ("true");        }else{System.out.println ("false");         } System.out.println ("---------------------");        Scenario Four System.out.println ("Scenario IV: Int vs. integer");        int a = 128;        Integer f = new Integer ("128");        if (a = = f) {System.out.println ("true");        }else{System.out.println ("false"); }     }}

The results of the operation are as follows:

Scenario One: Comparison of the values of the integer and new integer different initialization methods, integer and new Integer is not equal to false---------------------scenario two: Create two integertruefalse that are directly assigned---------------------Scenario three: Two are new integerfalse------ ---------------Scenario four: int vs. integer true

Summarized as follows:

Programme one:
I'll start by comparing the two objects, E and F, using the two different initialization methods of integer and new integer.

Integer E = 128;integer f = new Integer ("128"); if (E = = f) {    System.out.println ("true");} else{    System.out.println ("false");}

Return Result: false
It concludes that the integer is not equal to the new integer. The reference to e points to the heap, and F points to his memory (Chang), and their memory address is not the same, so false

Scenario Two:
Create two integers that are directly assigned, and compare the two objects;

Integer b = 127;//integer c = 127;if (b = = c) {    System.out.println ("true");} else{    System.out.println ("false");} Integer d = 128;//integer e = 128;if (d = = e) {    System.out.println ("true");} else{    System.out.println ("false");}

return Result:
True
False
It is concluded that two are integers that are directly assigned, or True if the number is between 128 and 127, otherwise false
Cause: Java is translated as Integer i2 = integer.valueof (128) When compiling integer i2 = 128, while the valueOf () function caches the number between 128 and 127

Look at the source code everyone will understand that for 128 to 127, the number will be cached, the integer b = 127, 127 will be cached, and the next time you write the Integer c = 127, it will be taken directly from the cache, will not be new.

/** * Cache to support the object identity semantics of autoboxing for values between * -128 and 127 (inclusive) a     s required by JLS.  * * The cache is initialized on first usage.     The size of the cache * May is controlled by the {@code-xx:autoboxcachemax=<size>} option. * During VM initialization, Java.lang.Integer.IntegerCache.high property * is set and saved in the private system     Properties in the * Sun.misc.VM class.        */private static class Integercache {static final int low =-128;        static final int high;         Static final Integer cache[];            static {//high value is configured by property int h = 127;            String Integercachehighpropvalue = Sun.misc.VM.getSavedProperty ("Java.lang.Integer.IntegerCache.high"); if (integercachehighpropvalue! = null) {try {int i = parseint (integercachehig                    Hpropvalue); I= Math.max (i, 127);                Maximum array size is integer.max_value h = math.min (i, Integer.max_value-(-low)-1);                } catch (NumberFormatException nfe) {//If The property cannot is parsed into an int, ignore it.             }} high = h;            cache = new Integer[(high-low) + 1];            int j = Low;             for (int k = 0; k < cache.length; k++) cache[k] = new Integer (j + +);        range [ -128, 127] must be interned (JLS7 5.1.7) assert Integercache.high >= 127; } private Integercache () {}}

Programme III:
I'm going to use the new integer initialization method to compare these two objects with the G and H objects;

Integer g = new Integer ("127"), Integer h = new Integer ("127"), if (g = = h) {    System.out.println ("true");} else{    System.out.println ("false");}

Return Result: false
Come to the conclusion that all two are new integer objects, pointing to different two integer objects, false

Programme IV:
I'll start by comparing the two objects, the A and F, using the two different initialization methods of int and new integer.

int a = 128;integer f = new Integer ("n"), if (a = = f) {    System.out.println ("true");} else{    System.out.println ("false");}

Return Result: True
It is concluded that int and integer (regardless of new No) are true because the integer is automatically removed as an int and then

Related articles:

Java Tutorial-int The difference from an integer

A detailed explanation of the differences between int and integer in Java

Related Article

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.