Comparison Operators and Autoboxingunboxing

Source: Internet
Author: User

######## Key points extracted from below ####################

1. Consider the following snippet of code:

Integer AA = new integer (100);
Integer BB = new integer (100);
Integer cc = new integer (505);
SYSTEM.OUT.PRINTLN (aa = = BB); would print false
SYSTEM.OUT.PRINTLN (aa = = cc); would print false

In this snippet of code, no autoboxing/unboxing takes place. here, aa = = BB and AA = = CC Compare the references of AA, BB and CC, not their values. Every object created with the new operator have a unique reference.


2. For integer.valueof () method, for all values between–128 and 127, the integer class caches integer object references


#####################################


Comparison Operators and Autoboxing/unboxing

I'll discuss comparison operations = =,, >=, <, <=. only = = (logical equality operator) can be used with both reference type and primitive types. The other operators must is used only with primitive types.

Let's discuss the easy ones (>=, < and <=) first. If A numeric wrapper object is used with these comparison operators, it must be unboxed and the corresponding primitive Ty PE used in the comparison. Consider the following snippet of code:

Integer a = 100;
Integer b = 100;
System.out.println ("A:" + a);
System.out.println ("B:" + B);
System.out.println ("a > B:" + (a > B));
System.out.println ("a >= B:" + (a >= b));
System.out.println ("A < b:" + (A < b));
System.out.println ("a <= B:" + (a <= b));

a:100
b:100
A > B:false
A >= b:true
A < B:false
A <= b:true

There is no surprise in the above output. If you mix the types, reference and primitive, with these comparison operators, you still get the same results. First, the reference type is unboxed and a comparison with the and the other primitive types takes place. For example,

if (101 > New Integer (100)) {
Do something
}

is converted to

if (101 <= (new Integer)). Intvalue ()) {
Do something
}

Now, let's discuss the = = operator and the autoboxing rules. If Both operands is primitive types, they is compared as primitive types using a value comparison. If Both operands is reference types, their references is compared. In these-cases, no autoboxing/unboxing takes place. When one operand was a reference type and another is a primitive type, the reference type was unboxed to a primitive type an D A value comparison takes place. Let's see examples of each type.

Consider the following snippet of code. It is a example of using both primitive type operands for the = = operator.

int a = 100;
int b = 100;
int c = 505;
System.out.println (A = = B); Would print true
System.out.println (A = = c); would print false

Consider the following snippet of code:

Integer AA = new integer (100);
Integer BB = new integer (100);
Integer cc = new integer (505);
SYSTEM.OUT.PRINTLN (aa = = BB); would print false
SYSTEM.OUT.PRINTLN (aa = = cc); would print false

In this snippet of code, no autoboxing/unboxing takes place. here, aa = = BB and AA = = CC Compare the references of AA, BB and CC, not their values. Every object created with the new operator have a unique reference.

Now, here's a surprise:consider the following snippet of code. This is relying on autoboxing.

Integer AAA = +;//boxing–integer.valueof (+)
integer BBB = n;//boxing–integer.valueof (+)
Integer c CC = 505; Boxing–integer.valueof (505)
Integer DDD = 505;//boxing–integer.valueof (505)
System.out.println (AAA = = BBB); Would print true
System.out.println (AAA = = CCC);//would print false
System.out.println (CCC = = DDD);//would prin T false

You used AAA, BBB, CCC, and DDD as reference types. What is AAA = = bbb true whereas CCC = = DDD false? All right. This time, there was no surprise coming from the autoboxing feature. Rather, it is coming from the Integer.valueof () method. For all values between–128 and 127, the integer class caches integer object references. The cache is used if you call its ValueOf () method. For example, if integer.valueof (+) twice, you get the reference of the same Integer object from the cache that Represents the int value of 100. However, if you call Integer.valueof (n), where n is outside the range–128 to 127, a new object was created for every call. This was the reason that AAA and BBB has the same reference from the cache, whereas CCC and DDD has different references . Byte, short, Character and Long classes also cache object references for values in the range–128 to 127.

Comparison Operators and Autoboxingunboxing

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.