Java basic Type Auto-boxing (autoboxing)

Source: Internet
Author: User
Tags wrapper java se

After the Java SE 1.5 release and later, the automatic boxing feature is available.

Auto-boxing, "wrapping" the underlying type into one type:

Basic Type-wrapper class

The following procedure triggers an automatic boxing process:

Integer count = 1;

Double amount = 2.0D

  

From the Javadoc API, the wrapper class provides a way to manipulate the underlying type of encapsulation:

Type conversions, such as float-to-int

Comparison

...

It is worth noting that for values between [-128, 127], automatic boxing uses caching to provide memory efficiency.

That is, automatically boxing the value of the range, the resulting wrapper class memory address is not changed.

For example

Integer a = 129;

Integer B = 129;

System.out.println (A = = B);//Output True

Practice:

1, automatic boxing appears in the variable declaration;

2, consider the use of one of the scenarios, the basic types need to compare, type conversion and other operations;

3, if you need to compare the wrapper class = =, it is recommended to convert to the underlying type, and then proceed.

To avoid the effect of the cache value range [-128, 127] on = = Comparison results.

Java basic Type Auto-boxing (autoboxing)

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.