-5

My question is simple. Whats the difference between -

s3=s1+s2 and s3="string" ?

I think I was very poor in explaining it.

I do understand the difference between == and .equals().

Tiny
  • 27,221
  • 105
  • 339
  • 599
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
  • A string object is not the same as a string literal. A literal is something that directly appears in your program; `s3` does not store a literal, but the result of an operation. – yshavit Oct 17 '13 at 06:05

5 Answers5

4

A new String object will be created, when the concatenation of 2 objects take place. But if you concat 2 string literals, then a new object won't be created.

String s3=s1+s2; // new object created
System.out.println(s3=="string"); // false

String s4="str"+"ing"; // this will not create a new string object
System.out.println(s4=="string"); // true

When the compiler encounters String s4="str"+"ing";, the compiler does a constant folding on the compile-time constants and puts it into just one string, since the concatenation happens at compile time itself, and the completed string therefore goes in the constant pool.

Rahul
  • 44,383
  • 11
  • 84
  • 103
  • +1. You might want to explain why it doesn't make a new object in the latter String. – Maroun Oct 17 '13 at 07:00
  • String s3=s1+s2; // new object created and s3 references the new object in the String pool right?. i.e, to say s3 points to the result of s1+s2.. right? – TheLostMind Oct 17 '13 at 07:02
  • Yeah, a new object is created and s3 references that. But in the case of s4, it references the string literal "string" which is already present in the constants pool. – Rahul Oct 17 '13 at 07:07
  • 1
    @R.J Too bad I can't revote.. :) – Maroun Oct 17 '13 at 07:10
  • Ya.. The edit helped.. Thanks.. But one more question... String s3=s1+s2; // new object created System.out.println(s3=="string"); // false .. Here "string" is not going into the string pool??.. – TheLostMind Oct 17 '13 at 07:21
  • 1
    Nope. It goes to the heap. But if you manually call `s3 = s3.intern()`, it'll lookup for your string literal in the constants pool and if found, will return the reference to that. This way you can make `s3` refer to the literal present in the pool. – Rahul Oct 17 '13 at 07:24
2

== operator checks whether the references to the objects are equal.

equals() method checks whether the values of the objects are equal.

For comparing strings use equals

System.out.println(s3.equals("string"));
newuser
  • 8,338
  • 2
  • 25
  • 33
  • My question is simple... Whats the difference between - s3=s1+s2 and s3="string" ? – TheLostMind Oct 17 '13 at 06:38
  • My apologies.. But that's not my question.. My question is what's the difference between the 2 statements.. BTW I understand the difference between == and .equals(). – TheLostMind Oct 17 '13 at 06:50
1

s3 is a new String object that is the concatenation of s1 and s2

== will compare their memory addresses. not their literal values. use .equals()

yamafontes
  • 5,552
  • 1
  • 18
  • 18
1

The function == checks whether the object of the to are the same.

Here the content is same but the objects are not same .

Look Here for a the difference between == and .equals

Community
  • 1
  • 1
Dileep
  • 5,362
  • 3
  • 22
  • 38
0

== is used on primitive types. On object you should use compare or equals. Strings are treatenin a particular way. Because == on strings doesn't alsways work fine is due by the concatenating of them by the jvm. It takes only the reference to the strings, == checks the memory location.

Emaborsa
  • 2,360
  • 4
  • 28
  • 50
  • `==` is used on objects, to check if they are the same instance. On objects one should use `equals` when one wants to check if two objects have the "same content" as defined by the particular implementation of `equals` on those objects (and if the objects don't override `equals`, `Object.equals` is used which just compares using `==` anyway). – SantiBailors Mar 07 '17 at 10:04