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?

bfs
  • 192
  • 2
  • 7
  • 1
    Related: http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – aruisdante Mar 17 '15 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. – vandale Mar 17 '15 at 21:35
  • When declaring a class property your declarations of x and y are equivalent. – IdusOrtus Mar 17 '15 at 21:38
  • Alright I get it thanks – bfs Mar 17 '15 at 21:41

3 Answers3

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";
Tim Hallyburton
  • 2,741
  • 1
  • 21
  • 29
  • 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 – bfs Mar 18 '15 at 00:29
  • This depends on the situation, sometimes it might be handy. Keep in mind that `String x;` is an uninitialized variable (not a NullPointer ) – Tim Hallyburton Mar 18 '15 at 05:56
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.

tzimnoch
  • 216
  • 1
  • 13
  • 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. – aruisdante Mar 17 '15 at 21:38
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.

Christoffer Hammarström
  • 27,242
  • 4
  • 49
  • 58