Java Automatic boxing mechanism and automatic unpacking mechanism __java

Source: Internet
Author: User
Tags wrapper

Java automatic boxing and automatic unpacking function

The first thing to note is the difference between the basic data type and the class, which is the difference between integers and int. Note that the container joins the object of the class rather than the base data type. The basic data types need to be packaged into objects first.

Integer i = 100; At compile time, the compiler will automatically perform the following syntax

Integer i = new integer (100); After the automatic boxing operation

public class AutoBoxDemo1

{

public static Voidmain (string[] args)

{

Integer i1= 127;

Integer i2= 127;

if (I1 ==i2)

System.out.println ("I1 = = I2");

Else

System.out.println ("I1!= i2");

}

}//This code will eventually display the correct, which is true, because for the number between-128-127, they are boxed in in memory after the integer object. and = = is used to denote the same object as comparison; If the value from –128 to 127 is exceeded, the boxed integer object is not reused, which is equivalent to creating a new integer object each time it is boxed. (Equals () compares the values (content) of two objects and = = = Compares two object references (memory address) is the same)

Integer i = 10; Packing

int t = i; Unboxing of course, in the operation, you can also automatically box and unboxing

System.out.println (i++);

The main differences for integers and int are:

Int I = 1; I do not have any method behind

Integer I = 1; I have a lot of ways behind it.

For a string type of case:

String str1 = "abc";

STRINGSTR2 = "ABC"; These two are references that point to the same object.

String Str3 =new string ("abc");

STRINGSTR4 = new String ("abc"); Generate two objects

The difference between the basic data type and its wrapper class is basically two-point difference:

Integer is the wrapper class of int, the initial value of int is 0, and the initial value of integer is null

Java intvalue () and valueof (), which converts an integer type to an int type, while valueof converts the given parameter to an integer type

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.