Results 1 to 6 of 6
  1. #1
    souFrag is offline Member
    Join Date
    May 2008
    Location
    Santiago, Chile
    Posts
    8
    Rep Power
    0

    Default Comparing Strings

    Hi everyone,

    Why does this return true?
    Java Code:
    String s1 = "hola";
    String s2 = "hola";
    
    if(s1 == s2) return true;
    By the way... I'm teaching an introductory java course and I know everything about objects and memory allocation... I also know C, PHP, Assembly, Java, etc. I mention this so you don't start explaining me about objects and stuff like that :-)

    I just want to know why the code above returns true even though s1 and s2 are supposed to be 2 different String objects... I just don't get it. Is it a Java bug??

    Thx.

  2. #2
    souFrag is offline Member
    Join Date
    May 2008
    Location
    Santiago, Chile
    Posts
    8
    Rep Power
    0

    Default

    Never mind... I've found the answer:
    When the java compiler finds two or more string "constants" that are equals, it points them to the same reference.

  3. #3
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Quote Originally Posted by souFrag View Post
    Hi everyone,

    Why does this return true?
    Java Code:
    String s1 = "hola";
    String s2 = "hola";
    
    if(s1 == s2) return true;
    By the way... I'm teaching an introductory java course and I know everything about objects and memory allocation... I also know C, PHP, Assembly, Java, etc. I mention this so you don't start explaining me about objects and stuff like that :-)

    I just want to know why the code above returns true even though s1 and s2 are supposed to be 2 different String objects... I just don't get it. Is it a Java bug??

    Thx.
    When you are creating String object like this.....String s1 = "hello";......then the compiler first checks in HEAP ....if same object is already there it points new to the existing one. But if you create like .... String s1 = new String("hello");.....it will always create a new String object.

    intern() method is also there to check for existing Objects(new String("hello"))....in HEAP....

    String s = "helo";
    String s1 = new String("hello");

    if(s == s1).......gives false

    s1.intern();

    if(s == s1).......gives true


    Compiler does this to save memory.

    and == operator checks only references(two objects are pointing to the same Object) so it returns true.


    sanjeev,संजीव

  4. #4
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    This question was answered a few times before. It is always a good idea to search the forums for answers before you ask.
    Daniel @ [www.littletutorials.com]
    Language is froth on the surface of thought

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    And the Google too. You can found lots of information regarding this. Most people confused with this each day.

  6. #6
    souFrag is offline Member
    Join Date
    May 2008
    Location
    Santiago, Chile
    Posts
    8
    Rep Power
    0

    Default

    Of course I tried searching :-)

    But the only answers I found were "you must use equals() to compare Strings" and that was not my question... I wanted to know why sometimes using "==" to compare to String objects would return true even though they seemed to be different objects.

    sanjeevtarar, thanks for your answer.

Similar Threads

  1. Comparing Images
    By shaungoater in forum Advanced Java
    Replies: 0
    Last Post: 03-17-2008, 11:38 AM
  2. JSTL -- Comparing two strings for equality
    By trinkets in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 02-12-2008, 05:39 PM
  3. Comparing Strings
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 10:44 AM
  4. comparing
    By Feng in forum New To Java
    Replies: 2
    Last Post: 11-23-2007, 10:40 AM
  5. Comparing JavaWebFrameworks
    By pegitha in forum Web Frameworks
    Replies: 1
    Last Post: 05-18-2007, 07:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •