Java Learning Note-Automatic unboxing (autoboxing&unboxing)

Source: Internet
Author: User

I. Basic Type Packager

1. Basic type: Long, int, double, float, Boolean

2. Class type: Long, Integer, Double, Float, Boolean

Difference: Basic types are more efficient, and objects of class type can carry more information.

public class TestInteger01 {public static void main (string[] args) {int a = 10;int b = 20;integer a = new Integer (a); Integ Er B = new Integer (b); System.out.println (A/3); System.out.println (A.doublevalue ()/3); System.out.println (A.compareto (B));}} /* Output Result: 33.3333333333333335-1*/

Doublevalue () can return a packaged value as a double type

CompareTo () can be compared to another integer object with the same 0, less than-1, and greater than 1

second, automatic unpacking and packing
Integer A = ten;  Auto-boxing int a = A;   Automatic unpacking
the inside of the automatic unpacking and boxing

Looking at the first example, this example declares the int and integer two types, and "= =" for the comparison whether the reference is to the same object

public class TestInteger02 {public static void main (string[] args) {int a = 100;int B = 100;if (A = = b) {System.out.println ("int basic type: a = = B");} ELSE{SYSTEM.OUT.PRINTLN ("int base type: A! = B");} Integer c = 100;integer d = 100;if (c = = d) {System.out.println ("Integer class type: c = = d");} Else{system.out.println ("Integer class type: c! = d");}}} /* Output Result: int basic type: a = = Binteger class Type: c = = d*/

As a result, a and b,c and D are all the same objects.

Looking at the second example, we changed the ABCD value to 200, and the output had unexpected results.

public class TestInteger03 {public static void main (string[] args) {int a = 200;int B = 200;if (A = = b) {System.out.println ("int basic type: a = = B");} ELSE{SYSTEM.OUT.PRINTLN ("int base type: A! = B");} Integer c = 200;integer d = 200;if (c = = d) {System.out.println ("Integer class type: c = = d");} Else{system.out.println ("Integer class type: c! = d");}}} /* Output Result: int basic type: a = = Binteger class type: c! = d*/

We found that at this point A and B are still the same object, and C and D are no longer the same object!

What is the reason for this? We can view Java/lang/integer.java

public static integer valueOf (int i) {        return  i >= | | < -128? New Integer (i): Small_values[i + 128];    }

This code means that in the integer class, when the value passed in is between (-128-127), the cache is checked for the same value, and if so, returns directly if it is not created on new.

When the passed-in value is not in the range (-128-127), it is created directly from new. So C and D are 200, already out of range, so they each open up a piece of memory space to store data, so it does not reference from the same object.

The basic type int, there is no such annoyance, each pass value, will see whether the cache already exists.

Understanding this, we also understand the difference between the basic type and the string type.

In this essay (the difference between the keyword super and this one in Java), I initially introduced the difference between the basic type and the string type, but did not understand it in depth, and today I saw Mr. Lin Shinliang's Java learning notes, finally there is a kind of clairvoyant feeling.

Java Learning Note-Automatic unboxing (autoboxing&unboxing)

Related Article

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.