Two Laptop Bag
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes problem regarding reference cast Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply 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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: problem regarding reference cast
 
Similar Threads
Regarding Widening and Boxing
regarding Boxing and Widening
boxing and widening doubt cant understand the logic for this program
Overloading when Combining Widening and Boxing
getting confused