This week's giveaway is in the Testing forum.
We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line!
See this thread for details.
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Reg. String and null

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If i have an variable say :
String s = null;
Now null is an reserved literal.which is the default value for Strings and boolean . Fine.
How do i check (for the above) that whether this string
is null or not ?
Doing the below check will give me a NullPointerException

So i finally do a check as


In String class, equals is used for comparing the contents
and == is used for comparing object references.


I am not clear that how null is treated and compared as in //1, //2 , //3.
Also whether null is taken as an object or string.
Pls explain.
thanks
 
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Angela : "....null is an reserved literal.which is the default value for Strings and boolean ..."
Gagan : No , Angela , null is a special literal , agree , but it is not default value for boolean ? It is default value for object references which are not local ( class members and object members )
Angela : "....whether null is taken as an object or string...."
Gagan : String is a Object itself ,not primitive type , and null is considered of no-specific-type itself ( it is not a Object of any Class , tho can be casted to any Class Type )

if( s.equals(null) ) //1 exception
Okay , fine here we are passing a null to a method which expects a Object , so we get a nullpointer exception at runtime.
if( s.equals("null") //2 exception
"null" is NOT same as null , "null" is a valid String , whose value is String "null"
String s1=null; // string-reference s1 is null
String s2="null"; // string-reference s2 points to a string-object which contain string "null"
String s3="Java"; // string-reference s3 is points to a string-object which contains string "Java"
if ( s == null ) //3 works out fine
Of Course , here we are comparing string-reference s , with literal null , which happen to be the value of reference s , so it returns true
Here is wht API-Doc say for String.equals()


Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.


Good Luck !

------------------
Gagan (/^_^\)
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Gagan, for the explanation.
Sorry for the stmt regarding null as default value for boolean .
I have one more doubt w.r.t to the API explanation as marked in bold below :


Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.


Does "Not null" mean not null literal..
 
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey am sorry to intervene but I think should point out about what I think is not correct.

Originally posted by Gagan Indus:
if( s.equals(null) ) //1 exception
Okay , fine here we are passing a null to a method which expects a Object , so we get a nullpointer exception at runtime.


No, you don�t get NullPointerException by passing null to the method where an object is expected, you are getting this exception cause you are calling a method using a reference which is null.
(you should never call a instance method using a null reference, if you do you will get NullPointerException and it has nothing to do with equals or String class).
Here you must note that it is allowed to pass null in equals() method, irrespective of the type of caller class/object.

Originally posted by Angela Narain:
I have one more doubt w.r.t to the API explanation as marked in bold below :

Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

Does "Not null" mean not null literal.


it just means that you should not use String.equals() methods to check whether it is null. If you do, it will always return false.
 
Gagan Indus
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Angela n Amit !
i was entirely wrong while saying this...


if( s.equals(null) ) //1 exception
Okay , fine here we are passing a null to a method which expects a Object , so we get a nullpointer exception at runtime.


Thankx Amit , for waking me up !
i culdnt hav been more dumb , gosh i hav to do part wid this hurry-thing or i ll losse marks on da D-dayz , where i shld nt
God bless me !

------------------
Gagan (/^_^\)
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess Gagan, i took time understanding that...
But i suppose all we can remember is not to invoke
any methods on an object if the object reference is null.

Correct me if wrong .
 
You have to be odd to be #1 - Seuss. An odd little ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic