aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes problem regarding reference cast 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 "problem regarding reference cast" Watch "problem regarding reference cast" New topic
Author

problem regarding reference cast

rooney kumar
Greenhorn

Joined: Jun 23, 2009
Posts: 2
hi all,
in the code:-
class Boxandwide
{
static void go(object o)
{
Byte b2=(Byte) o;
system.out.println(b2);
}
public static void main(Strin [] args)
{
byte b=5;
go(b);
}
}
shouldnot the output be a compiler error as object reference is cast into byte,when byte class itself inherits from object class?
Sridhar Gudipalli
Ranch Hand

Joined: Nov 02, 2005
Posts: 120
"....shouldnot the output be a compiler error as object reference is cast into byte,when byte class itself inherits from object class? ...."

No. You can cast Object reference to any other class..
It is not a compile error. But, it may throw a run time exception (java.lang.ClassCastException) if the that passed object is not instance of the new object.

Sridhar Gudipalli|SCJP 6.0
SCWCD objectives
rooney kumar
Greenhorn

Joined: Jun 23, 2009
Posts: 2
yeah got it thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem regarding reference cast