File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Boxing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Boxing" Watch "Boxing" New topic


Quirino Gervacio

Joined: Aug 09, 2008
Posts: 23

Why? >= and == are both comparison operators right?

SCJP 6.0
Henry Wong

Joined: Sep 28, 2004
Posts: 20517

From the title of your topic, you already know that boxing has taken place -- which means that you are comparing objects, not primatives. If you want them to be treated as primatives, you will need to unbox the values.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ankit Garg

Joined: Aug 03, 2008
Posts: 9462

System.out.println(s >= i);
after compilation becomes
System.out.println(short1.shortValue() >= integer.intValue());

but in case of == unboxing is not done automatically... This is because the == operator is allowed on objects but not the >= operator. So on compilation the compiler knows that >= needs unboxing as >= is only allowed on primitive types but the == is allowed on objects.

The solution is
System.out.println((short)s == i);

Now the compiler knows that you are trying to compare primitives. so i is automatically unboxed.....

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
I agree. Here's the link:
subject: Boxing
It's not a secret anymore!