Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String initialization difference

Firstly apologies if this a very basic question, I'm just curious to know the difference between the following string definitions

String x= "hello";
String y = new String("hello");

I knew that in java String is a Class its neither a primitive nor a Wrapper(Correct me if this a misconception). Consider a class A, I've seen the following declarations for any class so far. i think A c; is valid and A a = new A(); is also valid. I'm confused with A a ="xyz"; this is how we declared a String as in above first type of definition. I'm sure that the above two definitions are absolutely different, like if i say x==y it returns false. I understand that y is the reference to the String object. What is x there, how is it stored in memory, interestingly i found that both x and y can access all the methods of String class.

Then what is the advantage of one over other.Can i know the applicability of each.

like image 746
srk Avatar asked Mar 04 '12 13:03

srk


People also ask

What is string initialization?

String initialization is one of the basic parts of programming. String initialization means assigning a value to the variable before it's used in the java program. String initialization can be done in two ways: Object Initialization. Direct Initialization.

Is it necessary to initialize string in Java?

out. println(monthString); In order to use a local variable in java it must be initialized to something even if that something is setting it equal to null .

What is the difference between creating string as new () and literal?

When we create a String object using the new() operator, it always creates a new object in heap memory. On the other hand, if we create an object using String literal syntax e.g. “Baeldung”, it may return an existing object from the String pool, if it already exists.


2 Answers

From the 2nd edition of Joshua Bloch's "Effective Java":

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";
like image 68
John Doe Avatar answered Oct 27 '22 11:10

John Doe


The first couple of answers you got are incorrect. There is a difference between the two statements. But first, the TL;DR version: Use String x = "hello"; in 99.99999% of situations.

The full answer:

The chief difference between those two is that in the first case, the string is implicitly interned; in the second case, it is not. This is a very real difference, although it only comes into play in select situations. So in the first case, if you have any other strings with the same series of characters ("hello"), your x will refer to the one shared object that is used in all of those places (this is useful, since String instances are immutable). In the second case, you're explicitly saying you need (for whatever reason) to have a String instance with that sequence of characters which is separate from any others. There are very, very few reasons to do that.

With regard to x==y, in Java you compare strings for equality using equals, not ==. The == operator when used with object references compares the references (e.g., do both variables point to the same object), not the object contents.

So: Prefer the first form to the second unless you have a good reason for doing the second.

like image 22
T.J. Crowder Avatar answered Oct 27 '22 12:10

T.J. Crowder