This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
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

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "instanceof operator" Watch "instanceof operator" New topic

instanceof operator

Willie Toma
Ranch Hand

Joined: May 11, 2001
Posts: 78
There must be a reference type after the instanceof operator.
By 'reference type', you mean something like Object, and not the variable representing an Object...?
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
You need to name the Class or Interface that you are asking if the object is an instance of it.
if (myObj instanceof String){
//do something Stringlike

"JavaRanch, where the deer and the Certified play" - David O'Meara
Junilu Lacar

Joined: Feb 26, 2001
Posts: 6333

reference instanceof ReferenceType
ReferenceType can also be an array reference type, e.g.
ref instanceof int[] // true if ref is an int[]
ReferenceType cannot be java.lang.Class (compile time error) except when reference is declared as an Object.

Junilu Lacar
Sun Certified Programmer for the Java� 2 Platform

Junilu - [How to Ask Questions] [How to Answer Questions]
I agree. Here's the link:
subject: instanceof operator
It's not a secret anymore!