This is the fifth day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021
Important note: This article is part of a series of interview questions. Please follow me to check out the series of interview articles. Gitee open Source address: gitee.com/mydb/interv…
Java is a strongly typed language, so all attributes must have a data type. Just like metro supermarket, you need a membership card to go in.
PS: Java 10 have the local variable type derivation, can use var to replace a specific data type, but in the bytecode stage, Java variables still has a specific data type, and a local variable type has many limitations and imperfections are derived, is not the mainstream application version, so don’t do further discussion here.
Getting back to the subject, to understand the difference between int and Integer, start with Java’s basic data types.
Basic data types
There are eight primitive types in Java. There are four integral types, two floating point types, a char character type for Unicode encoding, and a Boolean type for true and false values.
- Four types of integers: int, short, long, and byte
- Two floating point types: float, double
- The character type is char
- True or false type: Boolean
Basic data types refer to non-divisible atomic data types. The value of this type is stored directly in memory. The data can be accessed directly through the memory address, and the memory area can only store the value of this type.
Packaging classes and their functions
Because Java is designed with the idea that everything is an object, in many cases you need to operate as an object, such as hashCode() to get a hash or getClass() to get a class.
The role of the wrapper class
In Java, there is a wrapper class for each basic data type, and the wrapper class for int is Integer. The existence of the wrapper class solves the problems that basic data types cannot do, such as generic type parameters, serialization, type conversion, and high frequency data caching.
The base type | Packing type |
---|---|
int | Integer |
short | Short |
byte | Byte |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
The difference between int and Integer
The difference between int and Integer is mainly reflected in the following aspects:
- Different data types: int is the base data type, while Integer is the wrapper data type;
- The default values are different: int defaults to 0, while Integer defaults to null;
- Int stores data directly in memory, whereas Integer stores object references. When an Integer is new, it actually generates a pointer to the object.
- Different instantiation methods: Integer must be instantiated to be used, while int does not.
- Variables are compared differently: int can use == to compare two variables for equality, whereas Integer must use equals to compare two variables for equality.
conclusion
Integer is a wrapper class for int, and they differ in five main ways: data types, default values, in-memory storage, instantiation, and variable comparison. The existence of wrapper classes solves problems that basic data types can’t: generic type parameters, serialization, type conversions, high frequency data caching, and so on.
Reference & acknowledgements
Coding Efficiently: A Java Development Manual
Focus on the public number: Java interview real questions analysis, check out more Java interview questions.