java in the range INTEGER

public static void main(String[] args) 
	{
		Integer a = new Integer(1);//此处若使用new,则a,b比较==值必为false
		int c = 1;
		Integer b = 1;
 
		System.out.println(a == c); // true
		System.out.println(a == b); // false
		
		Integer f1 = 100, f2 = 100, f3 = 150, f4 = 150;
		
		System.out.println(f1 == f2); // true
		System.out.println(f3 == f4); // false

Why (f3 == f4); // false?

public class AutoboxingTest {
 
    public static void main(String args[]) {
 
        // Example 1: == comparison pure primitive – no autoboxing
        int i1 = 1;
        int i2 = 1;
        System.out.println("i1==i2 : " + (i1 == i2)); // true
 
        // Example 2: equality operator mixing object and primitive
        Integer num1 = 1; // autoboxing
        int num2 = 1;
        System.out.println("num1 == num2 : " + (num1 == num2)); // true
 
        // Example 3: special case - arises due to autoboxing in Java
        Integer obj1 = 1; // autoboxing will call Integer.valueOf()
        Integer obj2 = 1; // same call to Integer.valueOf() will return same
                            // cached Object
 
        System.out.println("obj1 == obj2 : " + (obj1 == obj2)); // true
 
        // Example 4: equality operator - pure object comparison
        Integer one = new Integer(1); // no autoboxing
        Integer anotherOne = new Integer(1);
        System.out.println("one == anotherOne : " + (one == anotherOne)); // false
 
    }
 
}
 
Output:
i1==i2 : true
num1 == num2 : true
obj1 == obj2 : true
one == anotherOne : false

It is noteworthy that third small example of this is an extreme case. obj1 and obj2 initialization automatic packing operations have taken place. However, in consideration of saving memory, JVM caches objects Integer -128 to 127. Because obj1 and obj2 are actually the same object. So compare that return true "==."
java at compile time Integer a = 100; is translated into -> Integer Integer.valueOf A = (100);
2. Compare the time is still an object of comparison
when the value range does not meet -128,127 time. Remember with: new, open up new memory space, do not belong to IntergerCache management area.

public static void main(String []args) {
    Integer a = 100;
    Integer b = a;//此时b指针指向值为100的堆地址  即a的堆地址,a==b成立
    a++;//此时a指向的值发生变化为101,a指针指向101的堆地址。而b任然指向100
    System.out.println(a==b);
}

Printing is false

An Integer type occupies 4 bytes, a byte representing 8-bit binary codes, thus accounting for a total of 32-bit Integer binary code. Removing the first sign bit, and the remaining 31 to represent the value.

The minimum value is -2 ^ 31, a maximum value of 2 ^ 31-1

If Integer in java is not a new Integer object, but direct assignment such as:

         Integer b1 = 12;

     Integer b2 = 12;

This situation is the same in the constant pool to open up a space to store 12, the point 12 are b1 and b2, as shown below:

Here Insert Picture Description

Then talk about, Integer buffer range, because it is not a new subject in the heap, then make a provision in the size range of its constant pool must have a specification called JSL (Java Language Specification, java language specification) ** Integer buffering constraints do, which is a predetermined range: - between (128-127) **

In the computer, the data is stored by twos complement, in Java code, we see the "0x80000000", "0x7fffffff" are complementary code form, get their true value by converting the original code.
Conversion formula:

  • When the original code is positive, the positive number of the original code, anti-code and complement the same.

Positive number: 1
Hara码: 0000 0000 0000 0000 0000 0000 0000 0001
anti-码: 0000 0000 0000 0000 0000 0000 0000 0001
补码: 0000 0000 0000 0000 0000 0000 0000 0001

  • When the original code is negative when the sign bit inverted to remove bitwise complement code to remove the sign bit bitwise plus 1.

Guess you like

Origin blog.csdn.net/qq_30242987/article/details/88571831