Home > Java > Javagetting Started > body text

Examples to explain the integer and int types in java

王林
Release: 2020-04-03 15:58:52
forward
2077 people have browsed it

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);
Copy after login
结果: true
Copy after login
Copy after login

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);
Copy after login
结果: false
Copy after login

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);
Copy after login
结果: true
Copy after login
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template