Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

instanceof operator

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please remember "instanceof" is correct NOT "instanceOf" (No capital "O" please!)
Jaypii
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jaypii.
 
Bill Howerton
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between instanceof and instanceOf. Please someone explain.
Thanks
VR
 
manal
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ..
instanceof is a keyword (u cant use it for naming)
but instanceOf could be anyth (a var name , a class name ) ..
thanks ...
manal ..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic