An article thoroughly understand Integer, the concept of the difference between new Integer () and an int

The basic concept of distinction

1, Integer is a wrapper class int, int is a kind of basic data types java

2, Integer variables must be instantiated before use, and do not need int variables

3, Integer object is actually referenced, when a new Integer, actually generates a pointer to the object; and int data value is stored directly

4, Integer Default is null, a default value is 0 int

v2-cc772d1010a690fffb48bb9bf7c3f3bf_hd.png

Compare Integer, new Integer () and an int

1, two new Integer () variable comparison is always false

Because the new generation of two objects that different memory address

v2-83910a49a292ca5307b9ea4cf458be28_hd.png

2, Integer variables and new Integer () variable comparison, is always false.

Integer variables as java object pointed constant pool, the new new Integer () variable points to the new object in the heap, the two different addresses in memory.

v2-a521ea0367cde2fcfc3c20d5e2361643_hd.png

3, two Integer variables comparison, if two variables interval between -128 to 127, the comparison result is true, if the value of two variables is not in this range, the comparison result is false.

v2-78d28fb6a2ce18fe42c6964db6853481_hd.png

analysis:

Integer i = 100 at compile time, will be translated into Integer i = Integer.valueOf (100), and the definition of the type Integer java valueOf follows:

v2-09936f633e879f8b951b6bc3f2043567_hd.png

java to the number of -128 to 127, is cached.

Therefore, when Integer i = 127, 127 will be cached, when the next write Integer j = 127, will be taken directly from the cache, the new will not.

4, int Integer variables, () when compared to new Integer, as long as the two values ​​are equal, then true

Because packaging and basic data types int Integer comparison, java int is automatically unpacked, and then compared, in fact, becomes two comparison variables int.

v2-6ae9fd669e04a04fb65777626aeaa5d8_hd.png

Example 1:

v2-1ad87bb709a3909718fd7227465a7333_hd.png

the answer is

v2-6b7825a96c0a7df850570632edafbf38_hd.png

Example 2:

v2-091452a677e7d93e6962b2cecf2beafd_hd.png

Example 3:

v2-57ce0a58db7700b426eaabc4221791d7_hd.png

The following output is false is:

v2-373e6c0821dcbfc012e7e685ad189e0c_hd.png

Resolution:

i01 == i02. Compare () I02 two values ​​i01.intValue 5959 -> true;

i01 == i03. Since 59 between -128 to 127, therefore, I01 and i03 assignment operator returns the same object. The same object is returned from chche, the same object address true;

i03 == i04. i03 from the cache value, i04 is the new new object, the two are not the same object, it is false.

i02 == i04. And a similar, true.

The answer is C.

Example 4:

The only difference of Example 3, is to change the values ​​of all 128.

v2-242a70f6bccb0252070e7d4ea53ef18a_hd.png

The following output is false is:

v2-33bf336c36bcc3737c47cad73d782e80_hd.png

The answer :

v2-64dc94536ac08695dfc7d6a4d6aae798_hd.png


Finally,
we welcome the exchange, like the point of a praise yo remember the article, thanks for the support!


Guess you like

Origin blog.51cto.com/14442094/2432485