2

What is the Difference between String s1="Hello" and String s1=new String("Hello") in Java?

If String s1="Hello" and String s2=new String("Hello"), will s1 == s2?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
CMQ_14
  • 31
  • 1
  • 1
  • 2
  • Well the '==' operator matches the references. Since in this example there are 2 elements s1 and s2 and as they have different references/addresses so the output of is s1==s2 will be false. But if you use 'equals()' the output will be true as the values will be compared and as per the question the values are same. – BIJAY Feb 12 '17 at 08:15
  • And if there are two strings namely s1 and s2, if String s1="Hello"; String s2="Hello"; Here s1==s2 gives a true output. – BIJAY Feb 12 '17 at 08:18

5 Answers5

3
String myStr = "hello";
String myStr1 = "hello";

These both will evaluate to true when compared via double equals. However, they are not equal but rather both point to the same "literal string" in memory. This is NEVER how you compare contents of a String so don't let this fool you.

String myStr = new String("hello");
String myStr1 = new String("hello");

Will evaluate to false because they both reference distinct objects with distinct memory addresses.

Always always always use myStr.equals(myStr1) when comparing String's contents for equality. Remember == only compared whether they reference the same object in memory.

Akash KC
  • 16,057
  • 6
  • 39
  • 59
Whoppa
  • 949
  • 3
  • 13
  • 23
  • +1 for relevant example.. – Akash KC Oct 22 '13 at 05:02
  • If String s3 ="Hello",will s3==s1? – CMQ_14 Oct 22 '13 at 05:07
  • Well it will appear as if they do in that scenario but it's not comparing them the way we want to compare them, which is by contents. We want to see if every character in s1 and s3 are the SAME not whether both reference variables point to the same place in memory. Don't get in a habit of comparing objects this way (a string is an object) because you will get erroneous results and later down the road it will confuse you. Objects are opaque (encapsulation and data hiding) so what defined an objects contents varies. For now just still with .equals() – Whoppa Oct 22 '13 at 05:12
3

Coding this:

String s1 = "Hello";

Causes the JVM to intern the String literal: Every usage of the same String literal will be a reference to the same object.

Coding this:

String s2 = new String("Hello")

Will always create a new String object.

So, will s1 == s2?

No, never.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
1

Here is a quote from Joshua Bloch's Effective Java regarding the use of "new String()" :

As an extreme example of what not to do, consider this statement:

String s = new String("stringette"); // DON'T DO THIS!

The statement creates a new String instance each time it is executed, and none of those object creations is necessary. The argument to the String constructor ("stringette") is itself a String instance, functionally identical to all of the objects created by the constructor. If this usage occurs in a loop or in a frequently invoked method, millions of String instances can be created needlessly. The improved version is simply the following:

String s = "stringette";

Shishigami
  • 441
  • 3
  • 7
  • 20
0

If String s1="Hello" and String s2=new String("Hello"), will s1 == s2?

Answer is: No.

Because, s1 and s2 are different object and string is immutable. So, s1 == s2 will be false and s1.equals(s2) will be true.

Masudul
  • 21,823
  • 5
  • 43
  • 58
0

Your question's answer is no, please find the reason below:-

  1. String s1 = "Hello". Here, hello string will be stored at a location and and s1 will keep a reference to it.
  2. String s2=new String("Hello") will create a new object, will refer to that one and will be stored at a different location.

The condition, s1==s2 will compare memory locations which are now 2 different locations in memory. So it will come false.

Bette Devine
  • 1,196
  • 1
  • 9
  • 23