Int
int is one of the 8 primitive data types (Primitive Type:boolean, Byte, short, char, int, float, double, long) that we often call integer numbers. Although the Java language claims that everything is an object, the original data is an exception.
Integer
Integer is the wrapper class for int, which has a field of type int that stores data and provides basic operations such as math, int, and string conversions. With the introduction of auto-boxing and auto-unpacking in Java5, Java can be converted automatically according to the context, greatly simplifying the related programming.
About the value cache for integers
This involves another improvement in the JAVA5. The traditional way to build an integer object is to call the constructor directly and create a new object directly. However, according to the practice we found that most of the data operations are concentrated in a limited, small range of values, thus adding a static factory method in Java5 value of, when called it will use a caching mechanism, resulting in significant performance improvements. By Javadoc, the default value for this cache is 128 to 127.
Supplement 1
Primitive data types and Java generics do not work together, meaning that primitive types and generic cannot be mixed, so Java designs The auto-boxing/unboxing mechanism, which is actually primitive The invisible conversion mechanism between value and object, otherwise, if the mechanism is not, the developer must manually explicitly convert each time, this is much trouble. However, primitive value and object each have their own advantages, primitive value in memory is values, so find primitive value of memory location, you can get the value; Unlike object, it is reference. Find the memory location of object, but also according to reference to find the next memory space, to generate more IO, so the computational performance than primitive value difference, but object has the ability to generic, more abstract, solve business problems programming efficiency. So the Jvav designer's original intention is this: if the developer wants to calculate, use primitive value, if the developer wants to deal with business problem, should use object, adopt generic mechanism. Java has a auto-boxing/unboxing mechanism anyway, and there is no need for developers to notice anything. In order to compensate for the lack of object computing ability, the static valueof () method is also designed to provide a caching mechanism, which is considered as a remedy.
What is the difference between Java int and integer?