Integer class learning portal

Integer class

It is a class is the basic data type of package int class.

The basic API

img

img

img

img

img


Integer class and the difference int

  1. Is a wrapper class Integer int, int is one of the eight basic types of data (byte, short, int, long, char, float, double, boolean)
  2. Integer is a class, the default value is null, int is the basic data type, the default value is 0;
  3. Integer represents the object with a reference point to the object, and int is the basic data type, the value is stored directly.

Integer automatic unpacking and packing

Automatic and automatic packing unpacking after JDK1.5 is the only function that is one among many java syntactic sugar, it is executed at compile time, based on the syntax of the code, when generated class file, decide whether unpacking and packing operation.

Autoboxing

Generally, we create a new class of time by keyword, such as:

    Object obj = new Object();

But for the Integer class, but we can do:

Integer a = 128;

Why can this way, by decompiling tool, we can see that the generated class file is:

Integer a = Integer.valueOf(128);

This is the basic data type of automatic packing, 128 is the basic data type, then parsed into the Integer class.

Auto-unboxing

Integer data will be assigned to the class represents the basic data type int, on the implementation of the automatic unpacking.

Integer a = new Integer(128);
int m = a;

Decompile generated class file:

Integer a = new Integer(128);
int m = a.intValue();

Briefly: autoboxing is Integer.valueOf (int i); automatic unpacking is i.intValue ();

Consider the following questions:

public static void main(String[] args) {
    Integer i = 10;
    Integer j = 10;
    System.out.println(i == j);//true
      
    Integer a = 128;
    Integer b = 128;
    System.out.println(a == b);//false
     
    int k = 10;
    System.out.println(k == i);//true
    int kk = 128;
    System.out.println(kk == a);//true
      
    Integer m = new Integer(10);
    Integer n = new Integer(10);
    System.out.println(m == n);//false
}

We use the decompiler Jad, the resulting code is as follows:

public static void main(String args[])
{
    Integer i = Integer.valueOf(10);
    Integer j = Integer.valueOf(10);
    System.out.println(i == j);
    Integer a = Integer.valueOf(128);
    Integer b = Integer.valueOf(128);
    System.out.println(a == b);
    int k = 10;
    System.out.println(k == i.intValue());
    int kk = 128;
    System.out.println(kk == a.intValue());
    Integer m = new Integer(10);
    Integer n = new Integer(10);
    System.out.println(m == n);
}

First, direct statement Integer i = 10, automatically packing becomes Integer i = Integer.valueOf (10); Integer i is automatically unpacking i.intValue ().

  1. The first print is true

    For i == j, we know that these are two Integer class, they are more use should be equals, here is the comparison with == address, then the result is definitely false, but in fact the result is true, why is this?

    We enter into valueOf Integer class () method:

    img

    分析源码我们可以知道在 i >= -128 并且 i <= 127 的时候,第一次声明会将 i 的值放入缓存中,第二次直接取缓存里面的数据,而不是重新创建一个Ingeter 对象。那么第一个打印结果因为 i = 10 在缓存表示范围内,所以为 true。

  2. 第二个打印结果为 false

    从上面的分析我们知道,128是不在-128到127之间的,所以第一次创建对象的时候没有缓存,第二次创建了一个新的Integer对象。故打印结果为false

  3. 第三个打印结果为 true

    Integer 的自动拆箱功能,也就是比较两个基本数据类型,结果当然为true

  4. 第四个打印结果为 true

     解释和第三个一样。int和integer(无论new否)比,都为true,因为会把Integer自动拆箱为int再去比较。

  5. 第五个打印结果为 false

    因为这个虽然值为10,但是我们都是通过 new 关键字来创建的两个对象,是不存在缓存的概念的。两个用new关键字创建的对象用 == 进行比较,结果当然为 false。

测试一下:

Integer a = 1;
Integer b = 2;
Integer c = 3;
Integer d = 3;
 
Integer e = 321;
Integer f = 321;
 
Long g = 3L;
Long h = 2L;
 
System.out.println(c == d);//true
System.out.println(e == f);//false
System.out.println(c == (a + b));//true
System.out.println(c.equals((a+b)));//true
System.out.println(g == (a+b));//true
System.out.println(g.equals(a+b));//false
System.out.println(g.equals(a+h));//true

反编译结果:

img

分析:

第一个和第二个结果没什么疑问,Integer类在-128到127的缓存问题;

第三个由于a+b包含了算术运算,因此会触发自动拆箱过程(会调用intValue方法),==比较符又将左边的自动拆箱,因此它们比较的是数值是否相等。

第五个对于 g == (a+b),首先计算 a+b,也是先调用各自的intValue方法,得到数值之后,由于前面的g是Long类型的,也会自动拆箱为long,==运算符能将隐含的将小范围的数据类型转换为大范围的数据类型,也就是int会被转换成long类型,两个long类型的数值进行比较。

For the sixth g.equals (a + b), Similarly a + b will first automatically unpacking and packing the results automatically, it is noted that equals operator does not carry out conversion. So is Long.equals (Integer), the result of course is false

For the seventh g.equals (a + h), the operator will typecast +, A + h after each int + unboxing is long, the result is long , and long for the automatic packing Long, Long for two equals judgment.

end:

When the time being to add ~ ~ ~ understand the right, after in-depth study

Guess you like

Origin www.cnblogs.com/He-Fan/p/11537009.html