aspose file tools*
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
Author

Boxing

Quirino Gervacio
Greenhorn

Joined: Aug 09, 2008
Posts: 23
Hi,



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


SCJP 6.0
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18825
    
  40

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.

Henry


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

Joined: Aug 03, 2008
Posts: 9302
    
  17



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: http://aspose.com/file-tools
 
subject: Boxing