Fully understand the differences and applications between java basic types and encapsulation types

1. Basic types can only be passed by value, and the encapsulation class corresponding to each basic type is passed by reference.

2. In terms of performance, basic types in Java are created on the stack, while all object types are created on the stack (object references 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 appearance of encapsulated classes is to make it easier to use some methods that basic types do not have, such as valueof(), tostring(), etc. in addition, if you want to pass a reference to an int object instead of a value, you can only use encapsulated classes.

The call efficiency of allocating memory on the stack and the efficiency of allocating memory on the heap are too poor. Although it is efficient to allocate memory on the stack, there is a memory leak problem in allocating memory on the stack. (this is a problem that ordinary programmers can't solve...) Java has used a very talented method to improve the efficiency of allocating memory on the heap. Nevertheless, Java is still slow. It is unlikely to be as fast as C + +, although he has always promised that one day the virtual machine will be as fast as machine code.

JDK5. 0 can be automatically encapsulated, that is, the basic data can be automatically encapsulated into encapsulation classes. The advantage of basic data types is fast speed (not involving object construction and recycling). The purpose of encapsulation classes is to better process the conversion between data. There are many methods and it is convenient to use.

Of course, the passing of encapsulation types is reference passing, such as

Integer a = new Integer(1);

Indicates that a reference a of integer type refers to a piece of memory, and the data in this memory is 1; What is stored in a is the reference (address) of this memory. When a is passed to other methods or objects, the reference of a is passed.

Conversion between types:

String b = "123456";

int c = Integer. parseInt(b);

Represents converting the string 123456 into an integer number, where parseInt is a static method and can be used directly

Another point is that in some cases, you need to use encapsulation classes, such as a collection list, which can only add objects, that is, objects, so it is certainly not possible to store numbers directly. You need to encapsulate numbers into encapsulation type 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)); JDK5. After 0, packets can be automatically encapsulated, so it can be abbreviated as list = new arraylist(); list. add(1); list. add(2); list. add(3); list. add(4);

The above article to fully understand the differences and applications between java basic types and encapsulation types is all that Xiaobian has shared with you. I hope it can give you a reference and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>