Automatic boxing and automatic unboxing of integer classes in Java __java

Source: Internet
Author: User
Tags numeric value

The so-called automatic boxing, is the basic data types with their corresponding reference type packaging, so that they can have the characteristics of the object. For example, we can wrap the int type as an integer type, and wrap the double type as a double.

The so-called unboxing, is to do the opposite with automatic boxing operation.

Automatic Boxing: When a type of object is needed, the base data type is automatically encapsulated in the wrapper class that corresponds to it.

The process of automatic unpacking: When a value is needed, the values in the boxed object are automatically extracted and there is no need to invoke Intvalue () and Doublevalue () methods.

public static void Main (string[] args) {
		Integer a=100;
		Integer b=100;
		Integer c=200;
		Integer d=200;
		System.out.println (a==b);//1
		System.out.println (a==100);//2 System.out.println
		(c==d);//3
		System.out.println (c==200);//4
	}

In Java, "= =" compares a reference to an object, not a value (value), and A,b,c,d is an integer object after automatic boxing, so "= =" compares its reference. According to normal thinking 1 and 3 should be false, but the result is:

True

True

False

True

2 and 4 is because the automatic unboxing, the comparison is the basic data type, so "= =" compared with its numeric value, the result number true.

For result 1, although the comparison is the object's reference, but automatically boxing, Java in the compilation of the integer a=100, is compiled into an integer a=integer.valueof (100). Therefore, the key lies in the ValueOf () method.



Look at the following example:

Integer aa=-128;
		Integer bb=-128;
		System.out.println (AA==BB);
		Integer cc=-129;
		Integer dd=-129;
		System.out.println (CC==DD);
		Integer ee=127;
		Integer ff=127;
		System.out.println (EE==FF);
		Integer gg=128;
		Integer hh=128;
		System.out.println (GG==HH);

The result of the above code output is:

True
false
true
Description returns True when the range assigned after the integer type is -128---127, or false.


Summarize:

In fact, in order to improve efficiency, Java has an array in the Integercache class that caches an integer object with values ranging from 128 to 127. When we call integer.valueof (int i), if the value of I is >=-128 and <=127, an object is returned directly from the cache, otherwise an integer object is new.


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.