Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does '<', '> ' work with wrapper classes while '==' doesn't ? [duplicate]

Tags:

java

I know that '==' doesn't work with values outside the range of [-128,127] as there is a cache maintained of Integer objects within this range and the same reference is returned if value is within the range. But why does '>', '<', '>=', '<=' give correct answers even outside the range ?

Integer a=150; Integer b=150; System.out.println(a==b); //returns false  Integer a=150; Integer b=150; System.out.println(a>=b); // returns true 

Why is this happening ?

like image 435
Gaurang Singhal Avatar asked Jun 08 '17 05:06

Gaurang Singhal


People also ask

What is the advantage of using wrapper classes?

The primary advantage of Wrapper Classes is that we need Wrapper objects to function with collections which is only possible with the help of Wrapper classes. As the wrapper classes have objects we can store null as a value. We could not store null in variables of primitive datatype.

Why all wrapper classes are immutable?

It is because all primitive wrapper classes (Integer, Byte, Long, Float, Double, Character, Boolean, and Short) are immutable in Java, so operations like addition and subtraction create a new object and not modify the old.

What are wrapper classes and why are they useful for Arraylists?

The wrapper class objects support synchronization in the case of multithreading. It allows various features of ArrayList such as dynamic sizing and implementation of associated functions which are only defined on class objects. Without a wrapper class it will be very difficult to implement an ArrayList.

Can we compare wrapper classes using == in Java?

Although Short and Integer (and Double) share the same parent class (Number) these types are completely different from each other. Integer IS-NOT-A Short and Integer IS-NOT-A Double. Instances of different types can never be the same, so the compiler doesn't allow you to compare them using the == operator.


1 Answers

The <, >, <= and >= operators are only defined for primitive types. Therefore using them on wrapper types causes the compiler to unbox the objects into primitives.

This means

System.out.println(a>=b); 

is equivalent to

System.out.println(a.intValue()>=b.intValue()); 

However, the == and != operators exist for both primitive types and reference types, so using them to compare two objects of primitive wrapper types compares the references instead of the primitive values they wrap.

As Holger commented, comparison of object references with == and != existed in the Java language before auto-boxing and auto-unboxing were introduced, but comparison with <, >, <= and >= was not supported for any reference types before auto-unboxing was introduced.

This means that in the early days of Java, the a>=b of your code snippet would not pass compilation (since a and b are not primitive numeric types). On the other hand your a==b snippet would still pass compilation and return false.

Changing the behavior of == and != for reference types that happen to be wrappers of numeric primitives would change the behavior of existing code, thus breaking backwards compatibility, which is probably the reason it wasn't done.

like image 127
Eran Avatar answered Sep 26 '22 23:09

Eran