• 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

reference variable downcasting

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello world,
This is my first post so please bear with me.

I'm studying for SCJP 6 using Kathy Sierra and Bert Bates' study guide.

My question is what is the impact of the instanceof operator in snippet of code below?

class Animal {}
class Dog extends Animal {}
public class DogTest {
public static void main(String[] args) {
Animal animal = new Animal();
if (animal instanceof Dog) {
Dog d = (Dog) animal;
}
}
}

This code compiles and run fine, however, if you omit the instanceof test it gives a ClassCastException.

Thanks in advance.
 
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Luckson Karikoga wrote:Hello world,
This is my first post so please bear with me.

I'm studying for SCJP 6 using Kathy Sierra and Bert Bates' study guide.

My question is what is the impact of the instanceof operator in snippet of code below?

class Animal {}
class Dog extends Animal {}
public class DogTest {
public static void main(String[] args) {
Animal animal = new Animal();
if (animal instanceof Dog) {
Dog d = (Dog) animal;
}
}
}

This code compiles and run fine, however, if you omit the instanceof test it gives a ClassCastException.

Thanks in advance.



You cant directly downcast, Dog is an Animal but Animal is not a Dog, use of instanceof tells JVM that yes i know that is wrong but allow me to do so,and please use code tag and mention source of post always.
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Luckson Karikoga wrote:Hello world,
This is my first post so please bear with me.

I'm studying for SCJP 6 using Kathy Sierra and Bert Bates' study guide.

My question is what is the impact of the instanceof operator in snippet of code below?

class Animal {}
class Dog extends Animal {}
public class DogTest {
public static void main(String[] args) {
Animal animal = new Animal();
if (animal instanceof Dog) {
Dog d = (Dog) animal;
}
}
}

This code compiles and run fine, however, if you omit the instanceof test it gives a ClassCastException.

Thanks in advance.



consider the following snippet of code that you have written above
Animal animal = new Animal();
if(animal instanceof Animal)
Dog d =(Dog)animal;

here as said by Saloni above, you are downcasting animal reference to Dog. Now cast is simply an instruction or say a request to the JVM that i know what i'm doing. that i know the animal reference is really of type Dog. it is my responsibility. but beware the cast can fail at runtime . had you not written the instanceof check it would have . so here instanceof is like a safety mechanism . in the if condition you are making sure that animal reference really points to Dog object. if it is then only to the cast , otherwise not.
 
Luckson Karikoga
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your replies.

My code in from page 117, with a little modification.

I understand that you acknowledge the risks with downcasting. However if you compile and run the original code from the book without the instanceof test, you run into a ClassCastException. If you modify the original code from book with the instanceof test the application will compile and runs fine.

My question is what is the other magic of instanceof besides testing for the IS-A relationship?
 
gurpeet singh
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Luckson Karikoga wrote:Thank you for your replies.

My code in from page 117, with a little modification.

I understand that you acknowledge the risks with downcasting. However if you compile and run the original code from the book without the instanceof test, you run into a ClassCastException. If you modify the original code from book with the instanceof test the application will compile and runs fine.

My question is what is the other magic of instanceof besides testing for the IS-A relationship?



instanceof operator will return true if IS-A test succeeds and false otherwise. there is no other magic. instanceof operator as the name suggest is used for IS-A relationship check. in your example it will return false since animal reference points to animal object and not Dog object. and as saloni pointed out Every Dog is an animal but not vice versa. so instanceof operator is there to avoid the exceptional condition when we do invalid downcast
 
Luckson Karikoga
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Gurpeet ,and Saloni. Its now clear. I appreciate your time spent.
 
Luckson Karikoga
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The downcasting won't run because the if test fails.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Luckson Karikoga wrote:The downcasting won't run because the if test fails.



Yes, now you're learning slowly.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Luckson Karikoga wrote:The downcasting won't run because the if test fails.


Right
 
Luckson Karikoga
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yah the ranch really helps.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Luckson Karikoga wrote:Yah the ranch really helps.


reply
    Bookmark Topic Watch Topic
  • New Topic