0

Given this code:

String first = "Hello world";

String second = first;

first = "Something else";

After the execution, will the variable second point to the same memory instance that the variable first pointed in the first assignment (the same "Hello world") or will it be a completely different memory region (another memory region which also says "Hello world") ?

I want to know if making multiple assignments like in the second line (String other = originalString) causes any performance loss or if it's as fast as assigning any other object.

3 Answers 3

10

After the execution, will the variable second point to the same memory instance that the variable first pointed in the first assignment (the same "Hello world") or will it be a completely different memory region (another memory region which also says "Hello world") ?

The same memory region.

Here's what you have at each stage:

String first = "Hello world";

Gives you:

+-------+           +---------------+
| first |---------->| "Hello world" |
+-------+           +---------------+

Then

String second = first;
+--------+
| second |----\
+--------+     |     +---------------+
               +---->| "Hello world" | (same memory as above)
+--------+     |     +---------------+
| first  |----/
+--------+ 

Then

first = "Something else";
+--------+           +---------------+
| second |---------->| "Hello world" | (same memory as above)
+--------+           +---------------+
+--------+           +------------------+
| first  |---------->| "Something else" |
+--------+           +------------------+
Sign up to request clarification or add additional context in comments.

Comments

2

Second is a reference variable. Like any reference variable, assigning it to another causes only the reference to be copied, not the object. The two reference variables point to the same memory.

I want to know if making multiple assignments like in the second line (String other = originalString) causes any performance loss or if it's as fast as assigning any other object.

You're not assigning an object; you're assigning a reference to an object. Copying a reference to a string is no more expensive than copying any other reference. Copying a reference variable is very cheap. Usually you can copy the reference values as required for readability and understandability, without significant effect on performance.

Comments

1

String literals are immutable in java. Once a String literal is created it is cannot be modified and is stored on a string constant pool. Here is how it will work in your case:

String first = "Hello world";

New string will be created on a constant pool and 'first' will point to it.

String second = first;

Both the references first and second will point to the same string object.

first = "Something else";

The String object 'Hello world' will not be modified in this case. Instead a new object 'Something else' will be created and 'first' will start pointing to it. While the 'second' reference keeps pointing to the object 'Hello world'.

Now lets say you create a new reference something like this :

String third = "Something else";

Reference 'third' will also start pointing to the same object as first, even though you have not assigned the reference value of first to it using '=' operator.

3 Comments

"String objects are immutable in java." True, but irrelevant to the code the OP posted. It could be a mutable object like a HashMap and the answer wouldn't change, as the OP's code is completely replacing the object reference, not trying to make a change to the object it refers to. "Once a String is created it is...stored on a string constant pool" Not true. This is only true for intern'd strings. String literals are intern'd, not all strings.
Yes i agree with what you say. But as a matter of fact I was speaking about string literals.
Yes engineers are precise. I have added the keyword 'literal' in the statement. :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.