File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem regarding reference cast

 
rooney kumar
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"....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.
 
rooney kumar
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah got it thanks
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic