Java Auto-Boxing traps

Source: Internet
Author: User

Automatic boxing and unpacking is a syntactic sugar in Java, which brings us some confusion when it comes to ease of use, see the following code:

1  Public classTestClass {2      Public Static voidMain (String args[]) {3Integer A = 1;4Integer B = 2;5Integer C = 3;6Integer d = 3;7Integer e = 321;8Integer f = 321;9Long g = 3L;Ten          OneSystem.out.println (c = =d); ASystem.out.println (E = =f); -System.out.println (c = = (A +b)); -System.out.println (C.equals (A +b)); theSystem.out.println (g = = (A +b)); -System.out.println (G.equals (A +b)); -     } -}

Let's see what the output of this piece of code should be.

The answer is:

T

F

T

T

T

F

Is the answer not surprising to many people? We hit analysis.

1. Let us first clarify the role of the "= =" and the Equals method.

"= =": If it is a basic data type, the values are compared directly, and if they are reference data types, their addresses are compared

The Equals method inherits from the object class and can override the implementation in the parent class when implemented. Take a look at the source code found in object Qeuals, its implementation is also the object's address comparison, at this time it and "= =" function the same. There are classes in the JDK class that cover the Equals () method of the Oject class, and the rule is: if two objects are of the same type, and the content is consistent, return True, these classes are:
Java.io.file,java.util.date,java.lang.string, packing class (integer,double, etc.).

2. Java's constant pool technology. The basic types of wrapper classes in Java, where Byte, Boolean, Short, Character, Integer, long implement the constant pool technique, (except for Boolean, are only supported for values less than 128). What do you mean? For data ranging from 128 to 127, Java generates this range of data in a constant pool, as long as the wrapper class that points to the data has the same address. Data that is not within this range will be on the heap new.

3. "= =" does not automatically unboxing when an arithmetic operator is encountered, and their equals method does not handle the relationship of data type conversions.

Therefore, for System.out.println (c = = d); They point to the same address in the constant pool and return true.

For System.out.println (E = = f); Their value is greater than 127, and returns false even if the values are the same but correspond to different memory addresses.

For System.out.println (c = = (a+b)); They point to the same address in the constant pool and return true.

For System.out.println (C.equals (a+b)); Their values are the same, and the same type, which returns TRUE.

For System.out.println (g = = (a+b)); They point to the same address in the constant pool and return true.

For System.out.println (G.equals (a+b)); Their values are the same but the types are different and return false.

You got that?

Java Auto-Boxing traps

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.