Results 1 to 4 of 4
  1. #1
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default String object comparison issue

    Hi all,

    Here goes my piece of code

    /****************************/
    class Simple1 {
    public static void main (String[] args) {

    String s1 = "Hello";
    String s2 = "Hello";

    String s3 = "Hello";
    String s4 = new String(s3);

    System.out.println(s1==s2);

    System.out.println(s3==s4);
    }
    }
    /****************************/

    I know that (s3==s4) will give false since s3 and s4 are different objects even though their contents are same.
    But for (s1==s2) i get true. Could anyone please give a reasoning.
    Ain't s1 and s2 different objects too?
    Shouldn't this comparison also return false?

  2. #2
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default

    your assumption is true on all Objects but not in String.
    All strings we created in a java program are stored in a location called string literal pool. when a new String is created it check whether pool contains same, and if found new object reference is assigned with that same reference.

    also note this is the reason behind immutability of string

  3. #3
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by subith86 View Post
    But for (s1==s2) i get true. Could anyone please give a reasoning.
    Ain't s1 and s2 different objects too?
    No, there are not really different objects. Have you heard of String pooling?

    What happens is, Strings are basically IMMUTABLE objects. When you create a String like,

    Java Code:
    String s1 = "Hello";
    A String object "Hello" gets created on the heap. Now if you try to create another String with same value "Hello", there won't be another String object creation, but your new String reference would refer to the ONLY "Hello" created on the heap.

    That's why when you do s1 == s2, they both actually refer to the ONLY same value String present on the heap. And that's why it returns true.

    Hope that's clear enough,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  4. #4
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Smile

    Thanks jomy and goldest,

    Searched more about string literal pool. Now understood the concept.
    For others who still have the doubt, this link may help
    What is String literal pool?

Similar Threads

  1. String to String Comparison being ignored
    By leonsbuddydave in forum New To Java
    Replies: 4
    Last Post: 01-07-2011, 12:25 AM
  2. String Comparison
    By evant8950 in forum Java Applets
    Replies: 6
    Last Post: 04-22-2009, 08:11 AM
  3. Object Comparison!
    By sajdutt in forum Advanced Java
    Replies: 6
    Last Post: 05-23-2008, 01:48 PM
  4. String comparison
    By abhiN in forum New To Java
    Replies: 2
    Last Post: 04-09-2008, 04:47 AM
  5. String comparison
    By sireesha in forum New To Java
    Replies: 1
    Last Post: 12-18-2007, 12:16 AM

Tags for this Thread

Posting Permissions

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