File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference bet "" and null

 
Phillipe Rodrigues
Ranch Hand
Posts: 165
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why do I get the result of the above code
as
AB : BC : null


what is the difference between 1&2 .
 
Gavin Tranter
Ranch Hand
Posts: 333
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"" is an emptry String. That is a String of charactors that has no charactors in it. Even though there are no charactors, the Object reference is pointing to somewhere in memory and is allocated on the heap.

null, means the String object dosnt actual represent anything in memory, thus the object reference is NOT pointing to somewhere in memory and these is NO allocation on the heap.

generally speaking, NULL and empty should be treated as two distinct definitions.
 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ab is a String reference that currently refers to a String object that contains no characters.
bc is a String reference that currently doesn't refer to any object.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are printing out the first String, followed by what you get from the 2dn String.
The 2nd String is null, so the + operator adds "null" to whatever it was going to print out. So it prints out AB: then thisfollowed by BC: null. And the bit it prints out before "BC: null" looks like nothing.

There are three possible String objects which beginners confuse
  • The null String, which is your bc
  • The empty String, which is a String with no content, "", like your ab
  • A space String containing a single space, " "
  • The "" String is NOT a null String; it actually exists. A null String is a reference to a String which does not point to any object.
     
    Phillipe Rodrigues
    Ranch Hand
    Posts: 165
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    What actually happens to print these outputs?
     
    Balasubramanian Chandrasekaran
    Ranch Hand
    Posts: 215
    Firefox Browser Linux VI Editor
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Rakesh,

    Have a quick look at your code.
    Originally posted by Rakesh Mestry:


    What actually happens to print these outputs?

    In your third if condition
    (i.e.,)if(BC.equals(null))
    You are trying to compare BC String object which is not instantiated to empty string that is why you are getting NullPointerException

    One more thing is that you should not do a comparison like (BC.equals(null)) Your if condition must be of the form (BC == null)
    [ April 28, 2008: Message edited by: Balasubramanian Chandrasekaran ]
     
    Amit Ghorpade
    Bartender
    Posts: 2851
    10
    Fedora Firefox Browser Java
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Just to add on, if you say something like


    you'll not get a NullPointerException, but if str1=null in above case you'll have it


    Hope this helps
     
    Guido Sautter
    Ranch Hand
    Posts: 142
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Actually, the problem is that invoking any method on an Object reference pointing to null will result in a NullPointerException, so bc.equals() will throw a NullPointerException regardless of what argument it gets.

    The argument being null is a minor problen, for the equals() methods of a lot of classes simply return false if the argument Object is null, but will not throw a NullPointerException.
     
    Vikas Kapoor
    Ranch Hand
    Posts: 1374
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hello Rakesh,

    Here is the quick difference between == and equals():

    == checks wheather two object refernces points to same instance or not.
    Whereas equals() compare the actual contents of two string objects.


    Example:



    Then i guess,
    If two object references pass equals test then they have also passed == test but the reverse is not true.

    Originally posted by Balasubramanian Chandrasekaran:

    One more thing is that you should not do a comparison like (BC.equals(null)) Your if condition must be of the form (BC == null)

    [ April 28, 2008: Message edited by: Balasubramanian Chandrasekaran ]


    Why is it so?
    [ April 28, 2008: Message edited by: Vishal Pandya ]
     
    Joanne Neal
    Rancher
    Pie
    Posts: 3742
    16
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Vishal Pandya:
    Then i guess,
    If two object references pass equals test then they have also passed == test but the reverse is not true.


    Should be
    If two object references pass == test then they will also pass equals test but the reverse is not true.
     
    Vikas Kapoor
    Ranch Hand
    Posts: 1374
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Joanne Neal:


    Should be
    If two object references pass == test then they will also pass equals test but the reverse is not true.


    It was a typo.
    Thanks for the correction Joanne.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic