• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

instance of operator

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


//ouput:no


//output yes.can anyone please explain the above two programs working for me to get.Both problems i have used ,object reference conversion rules.do i need to use anywhere explicit casting rules.if so ,please explain it for me to get.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The instanceof operator uses the true runtime type of the object, and returns a boolean based on whether the object's reference could be cast to the indicated type without throwing a ClassCastException.

In your second example, you have X myx = new Objective23(); The variable holding the reference is of type X, but the true runtime type of the object is Objective23 (because that's what was actually created with the constructor call).

So when evaluating x instanceof Objective23, the question is: Given the true runtime type of the object, could the reference be cast to type Objective23 without throwing a ClassCastException?
[ January 16, 2007: Message edited by: marc weber ]
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Here,how it is worked if we give the o as the referencetype only.For that o,How can we get the runtime Object to cast to NewCar.
please help me further on this since not getting this properly i have refereed a lot of materials.but still having trouble.
the output is nothing.so false is getting executed.but how.

[ January 17, 2007: Message edited by: Shiva Mohan ]
[ January 17, 2007: Message edited by: Shiva Mohan ]
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please help me.
 
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you checking if "null" is an instance of anything? It is a special case, as it is a object literal that is meant to represent nothing. If null is an option, you should check to see if it is null first.

The JVM won't complain when you try to cast "null" to any type of object (because there is nothing to cast), and will return that it is *not* an instance of any type of object (because there is no object).

Henry
[ January 17, 2007: Message edited by: Henry Wong ]
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[ January 17, 2007: Message edited by: Shiva Mohan ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic