File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

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: 6529

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!