Why is 1 == 1 is true but 1000 == 1000 is false When dealing with Wrapper Classes in Java?

For Explanation watch video

In Java, the behavior where 1 == 1 is true but 1000 == 1000 is false when dealing with wrapper classes can be surprising. This is due to how Java handles object comparison and the concept of object caching for certain values in wrapper classes.

Understanding Wrapper Classes:

Java has wrapper classes for primitive types (e.g., Integer for int, Double for double, etc.). These wrapper classes allow primitive types to be treated as objects, which is useful in situations where an object is required, like in collections.

Autoboxing:

When you compare two numbers using == and these numbers are of primitive types (like int), Java compares their values directly. However, when you compare two wrapper objects using ==, Java compares their references, not their actual values.

Caching of Wrapper Objects:

To optimize memory usage, Java caches instances of certain values of wrapper classes. Specifically:

  • For Byte, Short, Integer, and Long, Java caches the values from -128 to 127.

  • For Character, it caches values from 0 to 127.

  • For Boolean, it caches the true and false values.

When a value within this range is autoboxed (i.e., converted from a primitive type to its corresponding wrapper object), Java returns the same cached object. However, if a value is outside this range, a new object is created.

Example:

public class WrapperComparisonExample {
    public static void main(String[] args) {
        Integer a = 1;
        Integer b = 1;

        Integer x = 1000;
        Integer y = 1000;

        // This will print true
        System.out.println(a == b);

        // This will print false
        System.out.println(x == y);
    }
}

Explanation:

  1. Integer a = 1; Integer b = 1;
  • Since 1 is within the cached range [-128, 127], both a and b point to the same cached object. Therefore, a == b compares the same object references and returns true.

Integer x = 1000; Integer y = 1000;

  • 1000 is outside the cached range, so x and y are two distinct objects. Even though their values are the same, they are different objects in memory. Thus, x == y compares different object references and returns false.

Correct Way to Compare Wrapper Objects:

To correctly compare the values of wrapper objects, you should use the .equals() method, which compares the actual values rather than the object references:

public class WrapperComparisonExample {
    public static void main(String[] args) {
        Integer a = 1000;
        Integer b = 1000;

        // This will print true
        System.out.println(a.equals(b));
    }
}

Conclusion:

  • == checks for reference equality when used with objects, including wrapper objects.

  • For Integer (and other similar wrappers), values in the range [-128, 127] are cached, so == may return true for these values.

  • For values outside this range, == returns false because different objects are created.

  • To compare the values of wrapper objects, always use .equals() instead of ==.

Did you find this article valuable?

Support realNameHidden by becoming a sponsor. Any amount is appreciated!