Detailed explanation of the difference between Integer and int

  • 2021-09-04 23:59:15
  • OfStack

The essential difference between Integer and int is that Integer is the encapsulation class and int is the basic data type (this is nonsense).

The purpose of this paper is to give a more detailed comparison and illustration of the differences between Integer and int

Differences between Integer and int

The default initial value of Integer is null, while the initial value of int is int, which means that Integer can distinguish the difference between unassigned value and value 0, while int cannot express unassigned value, so int is not suitable to be filled as form data of web layer. (For example: 1 student takes an exam with a score of 0, and does not take the exam with a score of null) Integer is a class with many methods to use, while int can only do 1 basic ± */= operation The Integer variable must be instantiated first, and the int variable is directly used

About Integer

When explaining Integer, there are several small knowledge points mentioned below:

Unpacking and packing When to unpack: When to box when basic data types and reference data types are operated: When basic data types are assigned to reference data types "==" and "equals ()" "==": Numeric values are compared when comparing basic data types, and object heap memory addresses equals () are compared when comparing reference data types: "==" is used in the Object class, but most of the time we override it In the Integer class, "==" is used to compare whether the object addresses are the same, while equals () is overridden, which first determines whether the object in the parameter is of type Integer, and if so, whether the value is the same.

 public boolean equals(Object obj) {
  if (obj instanceof Integer) {
   return value == ((Integer)obj).intValue();
  }
  return false;
 }

A few notes

When comparing the values of the Integer variable with the int variable, as long as the values are equal, the result is true (because Java will automatically unpack and split Integer into int for comparison)

Integer a = new Integer(1);
int b = 1;
System.out.println(a == b);//true

Two Integer variables generated by new are never equal (because new generates two new objects with different memory addresses)


Integer a = new Integer(1);
Integer b = new Integer(1);
System.out.println(a == b);//false

The two Integer variables generated by a non-new are also not equal (because the Integer variable generated by a non-new points to objects in the Java constant pool, while the new variable points to new objects in the heap at different memory addresses)


Integer a = new Integer(1);
Integer b = 1;
System.out.println(a == b);//false

Comparison of Integer variables generated by two non-new: when the value is between-128 and 127, the result is true, and when the value is outside this interval, the result is false (because the storage range of Integer constant pool is [-128, 127], which is directly stored in the constant pool and points to the same address. Numbers outside this range require 1 new object to be created through heap memory. For example, Integer a=1 will write 1 into the cache, and the next time Integer b=1 is written, it will go directly from the cache instead of new, so the address obtained is the same)


Integer a = 1;
Integer b = 1;
System.out.println(a == b);//true

Integer a = 129;
Integer b = 129;
System.out.println(a == b);//false

Related articles: