Java question: equals or not?

Source: Internet
Author: User

On the surface, this is just a few basic problems. When you really understand it, you will find that: Ah, it was the same thing! The following are several Java puzzles. Mistakes will occur without careful analysis.

Equals or not?

See the following code:

Code snippet 1

 
  1. public static void main(final String[] args) {    
  2.     Integer a = new Integer(100);    
  3.     Integer b = 100;    
  4.     System.out.println(a == b);     
  5. }   

What is the output of this code? I believe many people will easily guess: false, because the addresses of objects A and B are different, it is false to compare them with "=. Congratulations! You are correct.

Let's look at the following code:

Code snippet 2

 
  1. public static void main(final String[] args) {    
  2.     Integer a = 100;    
  3.     Integer b = 100;    
  4.     System.out.println(a == b);     
  5. }   

You may answer, this is nothing different, so it is still false. Unfortunately, if you execute the above Code, the result is true.

The above code may surprise you. Well, let's look at the following code:

Code snippet 3

 
  1. public static void main(final String[] args) {    
  2.     Integer a = 156;    
  3.     Integer b = 156;    
  4.     System.out.println(a == b);     
  5. }   

Is the result true? Unfortunately, if you execute the above Code, the result is false.

Are you surprised? Then let's look at the following code:

Code snippet 4

 
  1. public static void main(final String[] args) {    
  2.     Integer a = Integer.valueOf(100);    
  3.     Integer b = 100;    
  4.     System.out.println(a == b);     
  5. }   

The final result, as you may have guessed, is true.

Why?

Now let's analyze the above Code. It can be easily seen that the ultimate goal of this series of code is to use "=" to compare two objects. In Java, if "=" is used to compare two objects and the result is true, the two objects are actually the same object, and false indicates two objects.

Now let's see why the above phenomenon occurs.

Let's first look at code snippet 4:The final running result is true, indicating that the objects A and B are actually the same object. But object A is created by calling the valueof method of integer, and object B is created by automatic packing. How can it be the same object? The problem lies in bytecode. After all, Java programs rely on virtual machines to run bytecode.

Use the tool javap in JDK to parse bytecode. The core part is extracted as follows:

 
  1. 0:  bipush  100    
  2. 2:  invokestatic    #16; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;    
  3. 5:  astore_1    
  4. 6:  bipush  100    
  5. 8:  invokestatic    #16; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;   

In the code, we only call the integer. valueof method once, but the bytecode contains two calls to the integer. valueof method. So where is the other time? It can only be called during automatic packing. Therefore, this code is equivalent:

 
  1. public static void main(final String[] args) {    
  2.     Integer a = Integer.valueOf(100);    
  3.     Integer b = Integer.valueOf(100);    
  4.     System.out.println(a == b);     
  5. }   

Now the problem is simple: Check the JDK source code and view the specific implementation of the valueof method:

 
  1. public static Integer valueOf(int i) {    
  2.     final int offset = 128;    
  3.     if (i >= -128 && i <= 127) { // must cache     
  4.         return IntegerCache.cache[i + offset];    
  5.     }    
  6.     return new Integer(i);    
  7. }   

The above code makes it clear that for numbers from-128 to 127, valueof returns the objects in the cache. Therefore, the same object is returned when integer. valueof (100) is called twice.

Let's take a look at code snippet 3: according to the above analysis, code snippet 3 is actually equivalent to the following code:

 
  1. public static void main(final String[] args) {    
  2.     Integer a = Integer.valueOf(156);    
  3.     Integer b = Integer.valueOf(156);    
  4.     System.out.println(a == b);     
  5. }   

Because 156 is not in the range from-128 to 127, both objects are created using the new INTEGER () method, so the final result is false.

Segment 1 and Segment 2 will not be analyzed in detail. I believe that readers can analyze them by themselves.

Finally, let's take a look at the question: Through the above analysis, we learned that the automatic packing of integers is achieved through integer. valueof (INT number). How can we implement automatic unpacking?

Link: http://www.blogjava.net/todayx-org/archive/2012/02/01/369115.html

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.