Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing primitive to wrapper object with == behaviour unexplained

I have a piece of code which I need to understand:

public static void main(String[] args) {     Character c = new Character('a');     Character cy = new Character('a');     char cx = 'a';      System.out.println(c == cx);     System.out.println(cx == cy);     System.out.println(c == cy); } 

Output:

true true false 

I am unable to understand why only the third statement is failing.

EDIT: This question is different to the .equals vs == question as this about primitive versus object comparison.

like image 800
Fr_nkenstien Avatar asked Apr 11 '16 09:04

Fr_nkenstien


People also ask

What is the difference between primitive and wrapper class?

Wrapper class creates an object and primitive does not create object. Wrapper classes are used with Collections to represent type. Wrappers have methods and can hold memory address/null and primitives hold default values. Primitives are fast compare to wrapper classes as there is no overhead of methods or object.

Can you compare primitive data types in Java?

Primitives. Like in other languages, we can compare the values of primitives with the < , > , <= , and >= operator. The same problems of floating-point data types apply to them, so be aware. Also, boolean isn't comparable except for equality with == and !=

Why it is required to represent the primitive data types as objects using wrapper classes?

Wrapper Class will convert primitive data types into objects. The objects are necessary if we wish to modify the arguments passed into the method (because primitive types are passed by value). The classes in java. util package handles only objects and hence wrapper classes help in this case also.

Which of the following are wrapper classes for primitive types?

Each primitive type has a corresponding wrapper: byte, short, int, long, float, double, boolean, char. Byte, Short, Integer, Long, Float, Double, Boolean, Character.

What is the difference between primitive type and wrapper type?

A primitive type is a predefined data type provided by Java. A Wrapper class is used to create an object; therefore, it has a corresponding class. A Primitive type is not an object so it does not belong to a class. The wrapper class objects allow null values.

Which primitive is always autoboxed to its wrapper type?

A primitive is always autoboxed to its wrapper type. Here, 0, which is an int literal, is autoboxed to a java.lang.Integer wrapper instance. Since java.lang.Long and java.lang.Integer are different classes, equals between them must return false.

How to convert primitive types to objects in Java?

Here, we have used the valueOf () method of the Wrapper class ( Integer, Double, and Boolean) to convert the primitive types to the objects. To learn about wrapper classes in Java, visit Java Wrapper Class.

What is wrapper class in Java?

Wrapper classes can be used to achieve that task. The difference between wrapper class and primitive type in Java is that wrapper class is used to convert a primitive type to an object and object back to a primitive type while a primitive type is a predefined data type provided by the Java programming language.


2 Answers

c and cy refer to different instances of the Character class (each time you invoke a constructor, you create a new instance), so comparing these references returns false.

On the other hand, when you compare either of them to the primitive cx, they are unboxed to char, and the char comparison returns true.

Had you used Character.valueOf('a') instead of new Character('a'), you would have gotten the same instance in both calls, and the reference comparison would have returned true (since valueOf returns a cached Character instance if the argument <= 127).

like image 176
Eran Avatar answered Oct 04 '22 03:10

Eran


 System.out.println(c == cx);  System.out.println(cx == cy); 

Since one is primitive and another is a wrapper class of it, unboxing happens and primitive comparison takes place (==).

Whereas:

 System.out.println(c == cy); 

is an Object comparison. Different instances are getting compared so == won't work in this case.

like image 38
Suresh Atta Avatar answered Oct 04 '22 05:10

Suresh Atta