aspose file tools*
The moose likes Beginning Java and the fly likes using the instanceof operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "using the instanceof operator" Watch "using the instanceof operator" New topic
Author

using the instanceof operator

Leroy J Brown
Ranch Hand

Joined: Dec 02, 2007
Posts: 71
I'm wondering if it is possible to use the instance of operator when the second argument will not be known at compile time. Using an actually instance of an object perhaps to retrieve the type of argument that instance of wants. like this:

Pseudocode:


That type of thing doable?

Thank you,

tjcr
Uli Hofstoetter
Ranch Hand

Joined: Nov 24, 2006
Posts: 57
What did happen, when you compiled and (if it compiled) executed your example?

You might also want to check out http://mindprod.com/jgloss/instanceof.html.

Regards,
Uli


SCEA5, Certified ScrumMaster
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

That isn't possible, but java.lang.Class has a method called isInstance. You should use that.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41506
    
  53
Wouldn't that be the same as



That wouldn't work for interfaces and subclasses, though.
[ December 03, 2008: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Ulf Dittmer:



That wouldn't work for interfaces and subclasses, though.


Yes and no. If you load the same class via two ClassLoaders, you can end up with objects for which the above is true, but object1.getClass() == object2.getClass() is false; Rob's "isInstance()" will correctly return false in that case.


[Jess in Action][AskingGoodQuestions]
Leroy J Brown
Ranch Hand

Joined: Dec 02, 2007
Posts: 71

Yes and no. If you load the same class via two ClassLoaders, you can end up with objects for which the above is true, but object1.getClass() == object2.getClass() is false; Rob's "isInstance()" will correctly return false in that case.


Ernest,

Why would this be "correctly" false? I would think that you would want this to return true if they are truly the same class...

Is it possible that if two different class loaders where used then they may have two classes who's implementations are very different but they have the same fully qualified class names? If that's the case then I could see why you would want this to return false. Otherwise I don't understand if the point of the exercise is to discover whether one class is the same as the other.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi Tristan,

It's correctly false because the JVM identifies a class not just by the FQN but by the [FQN, ClassLoader] combination. If you have a class like this:



Load it via two classloaders, create an instance from each of thsoe two loaded classes, and then call test() on one object passing the other as an argument, you'll get a ClassCastException. Even if they both came from the same class file.

This kind of thing is not just academic; itcomes up occasionally in Web and J2EE apps that run in containers that use ClassLoaders to partition the JVM.
Leroy J Brown
Ranch Hand

Joined: Dec 02, 2007
Posts: 71
Originally posted by Ernest Friedman-Hill:
Hi Tristan,

It's correctly false because the JVM identifies a class not just by the FQN but by the [FQN, ClassLoader] combination. If you have a class like this:



Load it via two classloaders, create an instance from each of thsoe two loaded classes, and then call test() on one object passing the other as an argument, you'll get a ClassCastException. Even if they both came from the same class file.

This kind of thing is not just academic; it comes up occasionally in Web and J2EE apps that run in containers that use ClassLoaders to partition the JVM.


Thank you very much for all the replies.

starting with something like my first post, getting a solid answer from Rob and then learning something deep about java like [FQN, ClassLoader] system for similarity is why I love this place.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using the instanceof operator