1. Basic types can only be passed by value, while the encapsulation class corresponding to each basic type is passed by reference.
2. From a performance perspective, the basic types in java are created on the stack, and all object types are created on the heap (the references of objects are created on the stack). for example
Integer i=new Integer(10); where new Integer() is created on the heap, and its reference Integer i is on the stack. The emergence of encapsulation classes is to more conveniently use some methods that are not available in basic types, such as valueOf(), toString(), etc. Also, if you want to pass a reference to an int object instead of a value, you can only use encapsulation classes.
The calling efficiency of allocating memory on the stack is much different from the efficiency of allocating memory on the heap. Although allocating memory on the stack is efficient, there are memory leaks in allocating memory on the stack. (This is a problem that mediocre programmers can't solve...) Java uses a very genius method to improve the efficiency of allocating memory on the heap. Despite this, Java is still slow. He is unlikely to reach as fast as C++, although he has been promising that one day virtual opportunities will be as fast as machine code.
JDK5.0 can be automatically encapsulated, that is, basic data can be automatically encapsulated into encapsulation classes. The advantage of basic data types is that they are fast (do not involve object construction and recycling). The purpose of encapsulation classes is mainly to better handle data conversion. There are many methods and are convenient to use.
Of course, the pass of the encapsulation type is a reference pass, for example
Integer a = new Integer(1);
Indicates that an Integer type reference a refers to a piece of memory, and the data in this piece of memory is 1; and what is stored in a is the reference (address) of this piece of memory. When passing a to another method or object, the reference of a is passed.
Conversion between types:
String b = "123456";
int c = Integer.parseInt(b);
Indicates converting the string 123456 into an integer number, where parseInt is a static method, which can be used directly
Another point is that in some cases, encapsulation classes need to be used, such as a collection List, which can only add objects, that is, Object. Then it is definitely not possible to directly store numbers. You need to encapsulate the numbers into encapsulated objects and then store them in the List, such as
List list = new ArrayList();
list.add(new Integer(1));
list.add(new Integer(2));
list.add(new Integer(3));
list.add(new Integer(4));
After JDK5.0, it can be automatically packaged, so it can be abbreviated as
List list = new ArrayList();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
The above article comprehensively understands the differences and applications of Java basic types and packaging types. This is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.