JAVA packaging class you don't know

Source: Internet
Author: User

In JAVA programming, a series of types are often used, such as int char double. You can think of them as "Basic" types.
They obviously need special treatment, because new puts the object in a "heap", but simple variables such as byte and char are not very efficient in the heap.
 
For basic type processing, java adopts the same method as C and C ++. That is, you do not need to use new to create a variable. Instead, you can create a variable that is not an automatic variable referenced. This variable directly stores "values" in the stack, so it is more efficient.
 
However, because the basic types are not derived from JAVA objects, many operations cannot be completed.
 
For example, the mutual conversion between basic data types can not be brake conversion, but the forced conversion will cause problems, for example, if the String type is converted to the int type, jdk provides the corresponding packaging class for the convenience of users.
 
[Java]
<Span style = "font-size: 18px;"> int a = 0;
String result = Integer. toString (a); </span>
Java packaging class is very flexible to use
[Java]
<Span style = "font-size: 18px;"> char c = 'X ';
Character ch = new Character (c); </span>
You can also use
[Java]
<Span style = "font-size: 18px;"> Character ch = new Character ('x ');
And reverse conversion:
Char c = ch; </span>
Example 2:
Second, sometimes a function needs to pass an Object variable, but you cannot pass the int type. What should I do?
[Java]
<Span style = "font-size: 18px;"> public void test (Object obj ){
} </Span>
You want to pass 5 in.
Test (new Integer (5 ));
To sum up, the packaging class has two usage: one is to convert between the basic types; the other is to facilitate the function to pass values; the other is to conveniently Replace the basic data type when objects are used in some places.

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.