A comprehensive understanding of the differences between Java basic types and package types and their application _java

Source: Internet
Author: User

1. The base type can be passed only by value, and the encapsulated class corresponding to each base type is passed by reference.

2. the basic type in Java in terms of performance is created on the stack, and all object types are created on the heap (the object's reference is created on the stack). Like what

Integer i=new integer (10); Where the new Integer () is created on the heap, and his reference Integer I is on the stack. Encapsulation classes appear to make it easier to use methods that are not available for basic types, such as valueof (), toString (), and so on. And if you want to pass a reference to an int object instead of a value, you can only use the encapsulation class.

The efficiency of invoking memory on the stack and allocating memory on the heap is far less efficient. Although allocating memory on the stack is highly efficient, there is a problem with memory leaks on the stack. (This is a problem that mediocre programmers can't solve ...). Java is a very talented way to improve the efficiency of allocating memory on the heap, although Java is still slow. He is unlikely to reach as fast as C + +, though he has been promising one day that virtual opportunities will be as fast as machine code.

JDK5.0 can begin to automatically seal the package, that is, the basic data can be automatically encapsulated into the package class, the basic data type of the advantage is fast (does not involve the construction and recycling of objects), the purpose of encapsulation class is to better deal with the conversion between data, many methods, and easy to use.

Of course, the delivery of a package type is a reference pass, such as

Integer a = new integer (1);

A reference to an integer type a refers to a block of memory in which the data is 1; And A is a reference to the memory (address), to pass a to other methods or objects, it is a reference passed.

Conversions between types:

String B = "123456";

int c = Integer.parseint (b);

Indicates that the string 123456 is converted to an integer number, where parseint is a static method that can be used directly

There is also a point, in some cases, need to use the encapsulation class, such as a set list, it can only add objects to the inside, that is, object, the direct deposit of numbers is certainly not, you need to encapsulate the number of encapsulated type objects and then save to 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.0 can be automatically sealed later, so it can be abbreviated to  
List list = new ArrayList ();  
List.add (1); List.add (2); List.add (3); List.add (4);

< Span class= "Apple-converted-space" >< Span class= "Apple-converted-space" >< Span class= "Apple-converted-space" >< Span class= "Apple-converted-space" >< Span class= "Apple-converted-space" >< Span class= "Apple-converted-space" >< Span class= "Apple-converted-space" >

The above comprehensive understanding of Java basic types and packaging types of differences and applications is small to share all the content of everyone, hope to give you a reference, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.