Detailed explanation of the difference between int and Integer in Java
- 2020-04-01 01:44:09
- OfStack
1. Data types in Java are divided into basic data types and complex data types
Int is the former and integer is the latter (that is, a class).
2. Initialization
int i = 1;
Integer i = new Integer(1); //(to look at integer as a class)
Int is the basic data type (procedural traces, but a useful addition to Java)
Integer is a class, an extension of int, that defines many transformation methods
Similar to: float float, double double, string string, etc.
For example, when you want to put something in an ArrayList, a HashMap, a built-in type like int, or double, you don't want to put it in, because the container is full of objects, so you need an enclosing class of these built-in types.
Each of the built-in types in Java has a corresponding override class.
The relationship between int and Integer in Java is tricky. The relationship is as follows:
1. Int is the basic data type;
Integer is the wrapper class of int.
3, int and Integer can represent a certain value;
Int and Integer are not interoperable because they have two different data types;
Examples:
ArrayList al=new ArrayList();
int n=40;
Integer nI=new Integer(n);
al.add(n);//Can not be
al.add(nI);//can