• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can something equal null?  RSS feed

 
Tony VanHorn
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I may be thinking unclearly, it is a tad early, but if i need to check a string to see if it is null, how would i go about doing this? My first guess was string.isNull(), but alas there is no method like that. So I went to my friend Google and everything I saw there said string==null. I guess I don't understand how this is possible. How can one compare the equallatiy of nothing? If it works, great, but I would sure love to understand the logic behind it.

Thanks for any enlightment,
Tony
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony,
null is a Java keyword that represents the absence of an object. Since == is a built in operator, the language Java itself checks to see if the values on both sides of the operator are null. If so, it returns true. If one of them is "something" (aka non-null), it returns false.

string.isNull() doesn't work because there is no string object to call isNull() on.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that variables in Java (apart from primitives like int) are object references, not objects. Sois just checking whether the String reference is currently referencing an object.
 
Tony VanHorn
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. That definately makes sense.

Tony
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!