Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

instanceof operator

 
Rippon Jalali
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to ask a question that whether we can use instanceof operator with arrays?
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sure, if you want to check of array is instanceof int[], use this

 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ajay Singh:

if(array instanceof int[].class)


That should be

if (array instanceof int[]) ...
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for correction!
 
John Bartlett
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just wondering whether there is a way to compare instances of two objects

e.g.


cheers,

John
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can't compare the way have done below. Howerver, you can use something like:

 
Christopher Elkins
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is exactly how you stated;
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12181
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Rippon Jalali
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the right hand should be any type, interface, array, class
 
Rippon Jalali
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic