1

If I create a string and don't initialize the value. Are we using the same string object or is it reassigning the string reference to the new string, which may look like changing the value.

class Foo()
{
String x;
String y = null;

x = "Hello";
y = "Hey";
}

My understanding as of now is that the x would not be creating an additional String object. But the y would be creating another String but would reassign it's reference from the null String to the newly created string "Hey". Is this correct?

4
  • 1
    Related: stackoverflow.com/questions/40480/… Commented Mar 17, 2015 at 21:35
  • It's compiler dependent: a smart one would get rid of the null assignment but a less intelligent one might leave it in. This of course assume you aren't using the string in between the two assignments. Commented Mar 17, 2015 at 21:35
  • When declaring a class property your declarations of x and y are equivalent. Commented Mar 17, 2015 at 21:38
  • Alright I get it thanks Commented Mar 17, 2015 at 21:41

3 Answers 3

1
String foo

itself is just a reference. Since it is a String-reference it is able to reference a String from what's called the 'String Pool', an implementation of String Intering. It however does not create a new String.
Neither does

String bar = null;

String bar is, just as foo, a String reference, but creating a reference does not create a new Object. The difference between foo and bar is that bar is assignet to null but null is surely not a String and therefore the JVM has no reason to create a new String.

Your last assumption however comes closer to what actually happens.

String foo = "Hello World";

does create a new String (assuming "Hello World" was not found in the String Pool yet).
You can split up this statement into three smaller statements:

  1. Create Reference
    String foo creates a new reference, capable of referencing a String Object
  2. Creating Object
    "Hello World", as a short form for new String("Hello World") allocates spaces on the Object Heap (in this case the String Pool) and actually creates the new Object.
  3. Assignment
    = finally assigns our Object with our reference.



To sum everything up: neither of

String x;
String y = null;

will create a new String. But both of those statements will

x = "Hello";
y = "Hey";
Sign up to request clarification or add additional context in comments.

2 Comments

So there would be no point in doing String y = null; in general right? Since when you compile it would treat String x; as null
This depends on the situation, sometimes it might be handy. Keep in mind that String x; is an uninitialized variable (not a NullPointer )
0

Strings are immutable in Java. Both: x = "Hello" and y = "Hey" will create a new String object regardless of how x and y were declared.

1 Comment

Even if they weren't immutable, the = operator in Java will always result in an assignment to a new pointer address. Any aliases of x would still reference the old value.
0

You need to understand the difference between a variable and an object.

String x is a variable that can reference a String object. Declaring a variable does not create an object.

"Hello" is a String object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.