Home >Java >Javagetting Started >Examples to explain the integer and int types in java

Examples to explain the integer and int types in java

王林
王林forward
2020-04-03 15:58:522124browse

Examples to explain the integer and int types in java

Comparison of Integer and Int types

The Integer type is an object class. It is a boxing package of the basic type of an int. When we call the integer object When, Integer will point to the address of the int basic type it wraps.

If you compare Integer with int type, the system will automatically convert Integer into int type. At this time, when we compare int type, we will automatically compare the value of the address instead of the memory. Compare. Observe the following example:

(Recommended tutorial: java quick start)

public static Integer getIntegerExample1 = 128 ;
public static int getIntExample1 = 128 ;
System.out.print("结果: ");
System.out.println(getIntegerExample1 == getIntExample1);
结果: true

When we compare two Integer types, then the system will Compare the memory addresses. Because the memory allocation addresses are different, the results are different. Observe the following example:

public static Integer getIntegerExample3 = 128 ;
public static Integer getIntegerExample_3 = 128 ;
System.out.print("结果: ");
System.out.println(getIntegerExample3 == getIntegerExample_3);
结果: false

However, we have another situation, that is, when the size of the Integer value is between -127-127 When , Integer will be selected directly from the constant pool. Then when you compare the values ​​​​of two Integer in the constant pool, it will indicate that the two Integer point to the same memory address.

public  static Integer getIntegerExample2 = 127 ;
public static Integer getIntegerExample_2 = 127;
System.out.print("结果: ");
System.out.println(getIntegerExample2 == getIntegerExample_2);
结果: true

Related video tutorial recommendations: java video tutorial

The above is the detailed content of Examples to explain the integer and int types in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete