newNovice
newNovice

Reputation: 55

Integer vs. int comparison

I am new to java. I am now learning the non-primitive Integer type in java. I know the following comparison is not valid and throws a compilation error -

String str = "c";
Char chr = 'c';
if(str == chr) return true;

The above code snippet gives me the - "Test.java:lineNumber: incomparable types: java.lang.String and char" errors.

But I found the following code snippet compiles fine -

int a = 1234;
Integer aI = 1234;
if(a==aI) return true; 

Here, a is primitive int and aI is non-primitive. So how they are comparable? I am new to programming, may be is there any thing I don't know.

Thanks

Upvotes: 4

Views: 4222

Answers (3)

Razib
Razib

Reputation: 11163

This is called unboxing. Here aI is non-primitive/reference type. Here Integer is an wrapper of primitive int. Its gives some extra easy to use manipulations over primitive int. Each primitive type for example (boolean, byte, char, short, int, long, float, double) has a corresponding wrapper type (Boolean, Byte, Character, Short, Integer, Long, Float, Double).

So when a is compared with aI, first aI is unboxed and become a primitive int and it's value is compared with the primitive int. That means it is equivalent to -

int a = 1234;
Integer aI = 1234;
int a2 = aI.intValue();
if(a == a2) return true;

And for the first comparison the comparison occurred between completely two different data types - String and char. In this case there is no rule defined in java to convert char to String or String to char by default.

Upvotes: 2

KajolK
KajolK

Reputation: 193

In first case the two data type are different. So they can not be compared. And in second case, the two data type also different, but the wrapper Integer is made to support primitive int. So JVM automatically done the conversion of wrapper (unboxing) Integer to int and then compare. So actually in the second case two primitive int are compared to each other at the end.

Upvotes: 0

rgettman
rgettman

Reputation: 178253

The second snippet demonstrates an unboxing conversion. Boxing conversion allows a primitive type to be converted implicitly to a specific object wrapper type, e.g. int <-> Integer.

When comparing with the == operator, if one operand is a primitive type and the other is such a wrapper type, an unboxing conversion is performed so the 2 primitive values can be compared.

The first snippet doesn't compile because there is no boxing/unboxing relationship between String and char -- the relevant relationship is Character <-> char.

Section 5.1.8 of the JLS specifies all unboxing conversions:

  • From type Boolean to type boolean
  • From type Byte to type byte
  • From type Short to type short
  • From type Character to type char
  • From type Integer to type int
  • From type Long to type long
  • From type Float to type float
  • From type Double to type double

Section 5.1.7 specifies all boxing conversions, all of which are the reverse of the above.

Upvotes: 4

Related Questions