This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Difference bet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference bet "" and null" Watch "Difference bet "" and null" New topic
Author

Difference bet "" and null

Phillipe Rodrigues
Ranch Hand

Joined: Oct 30, 2007
Posts: 165


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


what is the difference between 1&2 .


Thanks,
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
"" 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

Joined: Aug 05, 2005
Posts: 3432
    
  12
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.


Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
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

    Joined: Oct 30, 2007
    Posts: 165


    What actually happens to print these outputs?
    Balasubramanian Chandrasekaran
    Ranch Hand

    Joined: Nov 28, 2007
    Posts: 215

    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

    Joined: Jun 06, 2007
    Posts: 2716
        
        6

    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


    SCJP, SCWCD.
    |Asking Good Questions|
    Guido Sautter
    Ranch Hand

    Joined: Dec 22, 2004
    Posts: 142
    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

    Joined: Aug 16, 2007
    Posts: 1374
    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

    Joined: Aug 05, 2005
    Posts: 3432
        
      12
    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

    Joined: Aug 16, 2007
    Posts: 1374
    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.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Difference bet "" and null
     
    Similar Threads
    hashcode() and equals()
    permutation
    How to implement swap() routine in Java ?
    Threads,serialisation, static variables - java basic doubts
    Casting problem