1

I came across this code and got a bit confused

java.lang.String s = new String("hello");

Im not sure what variable s is being initialized as java.lang.String , and what is the purpose of this vs String hold = "hello". I tried to look through some some documentation but could not find anything.

user2980837
  • 49
  • 1
  • 3

5 Answers5

5

This is really bad programming style

java.lang.String s = new String("hello");

Remember that all classes in java.lang are imported automatically. If you have a class called String in the same package, it will also be imported but shadow the java.lang.String class. That might be a reason to fully qualify the type like

java.lang.String s;

But in this case, you could only ever assign a java.lang.String reference to it since that class is final and therefore cannot be extended. The conventional thing to do would be

java.lang.String s = new java.lang.String("hello");

If you were asking about

java.lang.String s = new String("hello");

vs

java.lang.String s = "hello";

then check out the other answers or the duplicate.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
3

Ok you need to know about constants pool (String literal pool)

When you do

String s = "hello";

you are actually setting up the variable in the constant pool

where as when you do

String s = new String("hello");

it creates a separate object in the constant pool.

read more about it here

http://docs.oracle.com/cd/E13150_01/jrockit_jvm/jrockit/geninfo/diagnos/garbage_collect.html

Acewin
  • 1,657
  • 4
  • 17
  • 36
2

Doing this:

String s = "hello";

is better than doing this:

String s = new String("hello");

The second method creates a string object when you do "hello", passes it to the String constructor, and then creates another identical String object. The first method only creates one object.

gla3dr
  • 2,179
  • 16
  • 29
  • 2 objects? that is a bit confusing... can you furture explain – user2980837 Nov 12 '13 at 19:50
  • The literal creates the first "hello" string, then the String constructor copies that string (Java strings are immutable), and the original is thrown away. There is a chance that all of this may be optimized away, but it is still pretty bad style. – DennisK Nov 12 '13 at 19:55
  • @user2980837 The String constructor `public String(String original)` is called a _copy_ _constructor_ because it creates a copy of the string passed to it. See the link that dkatzel posted in a comment on your original question. – gla3dr Nov 12 '13 at 19:58
0

First thing you must look about is the immutable feature of String in java. http://javarevisited.blogspot.in/2013/03/how-to-create-immutable-class-object-java-example-tutorial.html

String hold = "hello" uses the immutable nature of the string. So when you do,

    String hold2 = "hello"

both hold and hold2 refer to the same instance of "hello" string from the string pool. It means both refer to the same memory location. But with

   String hold2 = new String("hello");

there is a new string "hello" at a new location and hold and hold2 objects refer to different memory locations.

shallOvercome
  • 386
  • 2
  • 10
0

The first thing you should know is that Strings in java are immutable read THIS. Next when you do String s = "hello" behind the scene what happens is that compiler looks into the String Pool (a place where strings are saved in java) and checks if there is a hello string already there. If it is, it points reference s to same string object and returns(Read THIS). Now if you do String hold2 = new String("hello"); none of the above mentioned things happens. Even if you have hello string in String Pool it would still create a new string hello and point reference hold2 to it.

Community
  • 1
  • 1
Prateek
  • 1,916
  • 1
  • 12
  • 22