2
String obj = null;
obj= new String("Samuel");

//vs

String obj = null;
obj="Samuel";

Is there any difference between these two ways of initializing a String?

user207421
  • 305,947
  • 44
  • 307
  • 483
Lulzim Fazlija
  • 865
  • 2
  • 16
  • 37
  • I tried to get the hashcode for both cases and it prints exactly the same hashcode for both ways of initializing the obj. Seems like there is no difference???? – Lulzim Fazlija Oct 23 '11 at 22:34
  • 2
    Equality of hashCodes doesn't imply equality, and equality doesn't imply identity. – user207421 Oct 23 '11 at 22:37
  • possible duplicate of [Initializing a String in Java](http://stackoverflow.com/questions/7021027/initializing-a-string-in-java) – user207421 Oct 23 '11 at 22:40
  • Heh, it's kind of Zen. <<"Nobody home," I said finally, sure that the answer wasn't good enough. "That's odd," he said. "Who's conducting the search?">> String s = new String("Something")... but what is "Something"? – david van brink Oct 23 '11 at 22:43
  • Possible duplicate of [What is the difference between "text" and new String("text")?](http://stackoverflow.com/questions/3052442/what-is-the-difference-between-text-and-new-stringtext) – acm Sep 19 '16 at 18:57

3 Answers3

7

Yes. And always prefer the 2nd option.

The first one creates an unnecessary string instance. The string literal (two quotes around a string) creates a string object itself. Then, if you use the first option, another, unnecessary instance is created.

When you use only the string literal (the 2nd option), the jvm uses a table where it stores canonical string objects. So for all strings declared with "Samuel" there will be only one instance in the JVM. But if you use the String(str) constructor, you'll have more instances, which means more memory.

To answer a follow-up question in the comments: this is only valid for strings. All other objects are created through constructors, as they don't have a designated literal.
For example, you needCar car = new Car("honda", "civic"). Simply having ("honda, "civic") isn't a valid syntax - it can't be known what type are you creating.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • 2
    thanks, they're gone now. But for the record: don't trust when you see a "-1" on an answer just for giving another "-1" – Bozho Oct 23 '11 at 22:39
  • 2
    I feel like I see a lot of "sympathetic downvotes" around, both on questions *and* answers. – Jared Farrish Oct 23 '11 at 22:46
  • hmmm still I have some doubts, what about this case Car c=null; c=new Car("name","type"); Car c=null; c=("name","type"); this case it shows error the same way of initializing an obj./ Why?? Why this way Java doesnt allow to initialize an obj and why for the String it allows??? – Lulzim Fazlija Oct 23 '11 at 22:49
  • The string literal is only about strings. Everything else is invoked with normal constructors. – Bozho Oct 23 '11 at 22:52
4
String obj = new String("Samuel");
String obj1 = new String("Samuel");

//vs

String obj = "Samuel";
String obj1 = "Samuel";

in the first case obj==obj1 returns false

in the second case obj==obj1 returns true.

The reason for that is that in the first case you have two references to two different Objects. In the second case you have one object since Strings are immutable they are interned and drawn from the same pool.

Costis Aivalis
  • 13,680
  • 3
  • 46
  • 47
0

Thus, s1 == s2 will be false and s1.equals(s2) will be true. The difference between is that first type of declaring string is called "String Literal" and second type of declaring string is called "String object." String literal is used to optimize the use of memory and efficiently manages the memory.

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
Sam
  • 1