• Post Reply Bookmark Topic Watch Topic
  • New Topic

instanceof problem...scjp book page no.285  RSS feed

 
sapana jain
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class InstanceTest
{
public static void main(String arg[])
{
String a = null;
boolean b = null instanceof String;
boolean c = a instanceof String;
System.out.println(b+" "+c);
}
}

output is coming to be false false.second instanceof test i am no0t getting.please help how it is coming to be false.
 
abhishek pendkay
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sapana
the problem is this line
String a = null;
using 'null' to do a instanceOf check will always result in a false
try using String a = ""
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
null, in Java means "does not point to anything" or, in other words "No Instance" so instance of could never return true for a null value.
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
null is never an instance of anything. Pay attention to the object in question for instanceofs.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!