This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) 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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "instanceof operator" Watch "instanceof operator" New topic
Author

instanceof operator

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi,
I would like to know how I can use the instanceof operator to test if a reference is an instance of some array. I tried the following code, but it generates compiler error.
String[] sa = { "This", "is", "a", "test" };
System.out.println(sa instanceOf String[]);
Thanks for your help.
Vani
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by Vani, Kadur:
Hi,
I would like to know how I can use the instanceof operator to test if a reference is an instance of some array. I tried the following code, but it generates compiler error.
String[] sa = { "This", "is", "a", "test" };
System.out.println(sa instanceOf String[]);
Please remember "instanceof" is correct NOT "instanceOf" (No capital "O" please!)
Jaypii
Thanks for your help.
Vani

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Please remember "instanceof" is correct NOT "instanceOf" (No capital "O" please!)
Jaypii
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks Jaypii.
Bill Howerton
Greenhorn

Joined: Aug 18, 2000
Posts: 7
check your uppercase/lowercase it's instanceof not instanceOf. But you cans use it to check for arrays just fine: try this:
class Instance
{
public static void main(String[] args)
{
int [] i = {0,1,2,3,4,5};;
double [] d = {0.0, 1.0, 2.0, 3.0, 4.0};
String [] sa = {"This", "is", "a", "test"};

System.out.println("i: "+ (i instanceof int[]));
System.out.println("d: "+ (d instanceof double[]));
System.out.println("sa: "+ (sa instanceof String[]));
}
}
it outputs:
i: true
d: true
sa: true
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
What is the difference between instanceof and instanceOf. Please someone explain.
Thanks
VR
manal
Ranch Hand

Joined: Aug 17, 2000
Posts: 45
hi ..
instanceof is a keyword (u cant use it for naming)
but instanceOf could be anyth (a var name , a class name ) ..
thanks ...
manal ..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: instanceof operator
 
Similar Threads
instanceof operator
IllegalMonitorStateException
strings - marcus green mocks
Doubt with a thread question about notify() and wait() methods
Doubt with a thread question about "synchronized(sa)"