• 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

casting

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A{}
class B extends A{}
class C extends A{}

final class D extends B{
public void doMethod(){
A a=new B();
B b=new D();
C c=new C();
D d=null;
//Insert Here
}
}


d=(D)(B)a //how can thisline cause classcastexception
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It will compile fine, but it will issue a ClassCastException, because a refers to a B object, and B is not a D (it is the other way around.)

The first cast is OK, it is the second cast that is the problem:
(D)(B)a
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(B)a is valid since a is an object of class B. However (D)(B)a is not valid since you can not cast an object B to class D as you can not "cast down".

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

It will alway throw ClassCastException if i try to down cast a object.

But why trying to down cast a null object, it doesn't throw NullPointException in Runtime.

eg.
Object o = null;
Integer i = (Integer)o;

Could you please explain it?

Thanks,

Yuan
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Yuan,

The null literal is a special literal of the null type, which denotes that the reference it is assigned to doesn't point to any object.
A reference of any type can be assigned null, and that is why downcasting is working in that sense. Normally, for downcasting you need to consider the actual type of the object involved. But for a null reference there is no object pointed to, so you can cast a null reference to any type.
 
Yuan Du
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Ruben!

Yuan
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No problemo! Just remember that you don't cast objects, but references. Upcasting always works, but for downcasting you have to make sure that the actual object's type satisfies the Is-A relationship for the type you are attempting to downcast the reference pointing to that object to.
 
My first bit of advice is that if you are going to be a mime, you shouldn't talk. Even the tiny ad is nodding:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic