The difference between primitive data types and wrapper classes

Reprint URL: Click to open the link

1. Java provides two different types: primitive types (built-in types) and reference types (encapsulated types).

Basic type : boolean char byte short int long float double   

Package type  : Boolean Character Byte Short Integer Long Float Double

2, int is the basic type, directly store the value. Such as: int i = 5;

Integer is an encapsulated type, and a reference is used to point to this object when an object is created. Such as: Integer i = new Integr(5)

3. Java divides memory into two types: one is stack memory and the other is heap memory.
Some basic type variables and object reference variables defined in the function are allocated in the stack memory of the function, while the actual object is in the storage heap memory. which is:

int i = 5;//Allocate space directly on the stack 
Integer i = new Integr(5);//The object is in heap memory, and i (reference variable) is in stack memory. Memory allocated in the heap is managed by the automatic garbage collector of the Java virtual machine.
4.When you need to put things in ArrayList and HashMap, basic types such as int and double cannot be put in , because the containers are all loaded with objects ( object types ), which requires the encapsulation classes ofEach basic type in Java has a corresponding wrapper class.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325725121&siteId=291194637