• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem regarding reference cast

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah got it thanks
 
reply
    Bookmark Topic Watch Topic
  • New Topic