Why is 1000 = 1000 in Java false and 100 = 100 true?

Source: Internet
Author: User

Why is 1000 = 1000 in Java false and 100 = 100 true?

This is an interesting topic.

If you run the following code

 
 
  1. Integer a = 1000, b = 1000;  
  2.     System.out.println(a == b);//1 
  3.     Integer c = 100, d = 100;  
  4.     System.out.println(c == d);//2 

You will get

False
True

Basic knowledge: we know that if two references point to the same object, they are equal with =. If two references point to different objects, = indicates that they are not equal, even if their content is the same.

Therefore, the following statement should also befalse.

This is where it is interesting. If you want to seeInteger.javaClass, you will find an internal private class,IntegerCache.javaIt caches all integer objects from-128 to 127.

So it becomes, all the small integers are cached internally, and then when we declare something like --

 
 
  1. Integer c = 100; 

In fact, what it does internally is

 
 
  1. Integer i = Integer.valueOf(100); 

Now, if we look at the valueOf () method, we can see

 
 
  1. public static Integer valueOf(int i) { 
  2.       if (i >= IntegerCache.low && i 
  3.           return IntegerCache.cache[i + (-IntegerCache.low)]; 
  4.       return new Integer(i); 
  5.     } 

If the value range is-128 to 127, it returns the instance from the cache.

So...

 
 
  1. Integer c = 100, d = 100; 

Point to the same object.

That's why we write

 
 
  1. System.out.println(c == d); 

We can get true.

Now you may ask why cache is required here?

The logical reason is that the usage of small integers in this range is higher than that of large integers. Therefore, using the same underlying object is valuable and can reduce potential memory usage.

However, you may misuse this function through the reflection API.

Run the following code and enjoy its charm.

 
 
  1. public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException { 
  2.  
  3.       Class cache = Integer.class.getDeclaredClasses()[0]; //1 
  4.       Field myCache = cache.getDeclaredField("cache"); //2 
  5.       myCache.setAccessible(true);//3 
  6.  
  7.       Integer[] newCache = (Integer[]) myCache.get(cache); //4 
  8.       newCache[132] = newCache[133]; //5 
  9.  
  10.       int a = 2; 
  11.       int b = a + a; 
  12.       System.out.printf("%d + %d = %d", a, a, b); // 
  13.     } 

Http://www.codeceo.com/article/why-java-1000-100.html.
Why 1000 = 1000 Returns False, but 100 = 100 Returns True in Java?

Related Article

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.