0

When using String s1="java"; and String s2= new String("java"); do both of these create different String objects? I know if I use String s3="java" it uses the same object of s1 but in s2 case also does it use same object? If so then why does StringBuffer sb = new StringBuffer("java"); use a different object. Because if i do System.out.println(sb.equals( s1 )); it returns false;

My understanding of equals method is that it compares if both the references refer to same object unless we have overridden equals method , please let me know if my understanding is wrong.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
Anil
  • 119
  • 5
  • you are right with this. – Alex Aug 23 '13 at 14:44
  • String overrides the `equals` method to compare the string contents. However, StringBuffer does not. – Hot Licks Aug 23 '13 at 14:44
  • Might Helpful :[How can a string be initialized using “ ”?](http://stackoverflow.com/questions/17489250/how-can-a-string-be-initialized-using/17489410#17489410) – Suresh Atta Aug 23 '13 at 14:53
  • These link will help you http://www.thejavageek.com/2013/06/19/the-string-constant-pool/, http://www.thejavageek.com/2013/06/17/string-immutability-in-java/ – Prasad Kharkar Aug 23 '13 at 14:54

6 Answers6

8

does both of these create different String object

There are two aspects here. First one is interning of String literals. Out of these two statements:

String s1 = "Hello";
String s2 = new String("Hello");

First one will use the literal string "Hello" that is already in the constant pool. If it isn't there, it will create an entry in the constant pool for "Hello" (In which case you can say that, an object is created)

In 2nd case, you have 2 string objects - first one is the string literal "Hello", which will be interned from the constant pool, and then the 2nd object creation is due to the use of new keyword - new String(...) will certainly create a new object. So:

s1 == s2;  // This will return `false`

because, s1 and s2 are reference to 2 different String objects.


Now comes your 2nd case:

String s1 = "Hello";
StringBuffer sb = new StringBuffer("Hello");

In 2nd statement, you are creating a new StringBuffer object. Well, firstly a StringBuffer is not a String. Secondly, since StringBuffer class does not override equals() method, so when you invoke equals method like this:

sb.equals(s1);

it will invoke the Object#equals() method, which does comparison based on the value of reference. So, it will return false, since sb and s1 are pointing to 2 different instance.

However, if you compare them like this:

sb.toString().equals(s1);

then you will get true now. Since String class has overridden the equals() method, which does the comparison based on the contents.


See also:

Community
  • 1
  • 1
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
2

Your understanding of the equals method is wrong. It is the == operator that does what you are describing.

The equal method is implemented to do this (quoted from the String class documentation) :

"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."

Marc
  • 2,631
  • 1
  • 12
  • 13
  • Hmmm - *"The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, **this method returns true if and only if x and y refer to the same object (x == y has the value true)**."*. What you say is in only for String class. – m0skit0 Aug 23 '13 at 15:11
2
String s1="java";

At this point, s1 points to a String object.

String s2 = new String("java");

String has already overridden the equals method, to check for the contents of the object, as per the documentation.

SO s1.equals(s2) will evaluate to true, because they have the same contents.

Object.equals() will check if the two objects are the same.

christopher
  • 26,815
  • 5
  • 55
  • 89
2

String s1="java"; and String s2= new String("java"); does both of these create different String object

new always creates a new object. That said, the internal string they're referring to is the same. In this case == will return false but equals will return true.

if so then why does StringBuffer sb = new StringBuffer("java"); use different object

StringBuffer is not String. They're 2 totally different classes. It's the same as comparing String with Integer. Maybe you meant System.out.println(sb.toString().equals( s1 ));?

My understanding of equals method is that it comparse if both the reference refer to same object unless we have overridden equals method

You're right, but in this case, String overrides equals() (and hashcode() as well) so the behavior is not that of Object#equals().

m0skit0
  • 25,268
  • 11
  • 79
  • 127
0

Ok, let use an analogy.

If I write the same word like 'HELLO' on two pieces of paper.

Then I bring in a panel of experts and ask them some questions.

Q. Expert one. Are those two things the same? A. Yes they are the same, it says HELLO.

Q. Expert two, make me a paper airplane out of a piece of paper. A. Ok, sure.

Q. Expert three, are these two things the same? A. Of course not, one is a paper airplane.

Q. Expert four, get another sheet and write 'HELLO' on it. Now at all these things the same?

A. Of course, they all say 'HELLO'

So, it depends what you mean by equals.

And computer languages have some non intuitive ways of defining equals.

Sometimes equals means we care about the words on the paper, sometimes we are concerned that it the exact same piece of paper' which doesn't matter a lot of the time, as long as they both say 'HELLO'

Andyz Smith
  • 698
  • 5
  • 20
-1

s1 and s3 refer to the same object but s2 is another different object in memory. check out http://www.journaldev.com/797/what-is-java-string-pool the image and explanations at that link will clarify it more than words can.

LeandreM
  • 943
  • 3
  • 12
  • 24
  • THanks for all answeres , but still i am confused about what exactly equals method do let us not limit to Stirng class; – Anil Aug 26 '13 at 13:41
  • @Anil equals method compares the content of an object. If You understand objects references, the `equals()` method compares the content of objects but doesn't bother if the references refer to the same location in memory. You can create same references like this: `String str1=new String("Cat")` and `String str2=str1` in this case the `str1.equals(str2)` method will always return `true` as well as `str1==str1` . But when both strings are created and their contents **(meaning the value that is referenced)** are different the `equals()` method will return `false` – LeandreM Aug 28 '13 at 13:36