aspose file tools*
The moose likes Beginning Java and the fly likes 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 "instanceof operator" Watch "instanceof operator" New topic
Author

instanceof operator

Rippon Jalali
Greenhorn

Joined: Aug 25, 2006
Posts: 23
i want to ask a question that whether we can use instanceof operator with arrays?
Ajay Singh
Ranch Hand

Joined: Dec 13, 2006
Posts: 182
sure, if you want to check of array is instanceof int[], use this

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Ajay Singh:

if(array instanceof int[].class)


That should be

if (array instanceof int[]) ...


[Jess in Action][AskingGoodQuestions]
Ajay Singh
Ranch Hand

Joined: Dec 13, 2006
Posts: 182
thanks for correction!
John Bartlett
Ranch Hand

Joined: Jan 25, 2006
Posts: 116
just wondering whether there is a way to compare instances of two objects

e.g.


cheers,

John
Ajay Singh
Ranch Hand

Joined: Dec 13, 2006
Posts: 182
you can't compare the way have done below. Howerver, you can use something like:

Christopher Elkins
Ranch Hand

Joined: Oct 26, 2004
Posts: 45
It is exactly how you stated;


Christopher Elkins, SCJP Java 2 Platform
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Christopher Elkins:
[QB]It is exactly how you stated;


No, it's not. The second argument to instanceof is the literal name of a class, not variable data of any kind, and certainly not an object.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11479
    
  16

This doesn't make sense to me:

that's like saying

what kind of object is fred? is satchmo? fred is an instance of Person, but satchmo is an instance of FredsCat.

unless your in a jazz music program, then satchmo could be a Person.

You can only have an instance of a class (or interface).

I think what you are trying to get at is, given two object references, you want to know if they are of the same type. Is that what you are asking?
[ January 08, 2007: Message edited by: Fred Rosenberger ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rippon Jalali
Greenhorn

Joined: Aug 25, 2006
Posts: 23
actually the question was like this
# instanceof operator can be used with ...

1. interfaces
2. Arrays
3. Classes
4. All of the above

so should answer be the (4)
Ajay Singh
Ranch Hand

Joined: Dec 13, 2006
Posts: 182
Yes, the right hand should be any type, interface, array, class
Rippon Jalali
Greenhorn

Joined: Aug 25, 2006
Posts: 23
i checked it for:
if(array instanceof int[]).
it is working .And ofcourse the answer should be (4).Thank you for your response
John Bartlett
Ranch Hand

Joined: Jan 25, 2006
Posts: 116
Yes that is what i meant, my method gets passed two Objects and i want to be able to tell whether they are the same type.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: instanceof operator