2
    String a = "test";
    String b = a;

    a = "wuut";

    System.out.println(b);

Prints out test

Shouldn't b hold refence of a, not just take its value?

Doesn't Java work that way with objects and stuff?

1
  • 3
    Search for String Immutability on Google.. You will get so many examples Commented Oct 5, 2012 at 6:27

7 Answers 7

5

Shouldn't b hold refence of a, not just take its value?

No. The value of a is a reference. References are a way of getting to objects, not variables. The assignment operator just copies the value of the expression on the right hand side into the variable on the left hand side. That's all it does - it's very simple. There's no pass-by-reference in Java, no variable aliasing etc.

Sign up to request clarification or add additional context in comments.

3 Comments

But isn't String object? Or String is special object? When I create my objects, will it work then?
@Jaanus: Yes, String is an object. But the value of a isn't a String object. It's a reference to a String object. All your classes will work in exactly the same way.
So String is like singleton ? And it holds like lots of values or smthin? When I have MyObject a = new MyObject(), and when I create Myobject b = a, then it is reference, and changing a also changes b?
2

When a is created lets say it point to place memory_1 in memory.

When b is assigned to a, then b also points to the same memory_1 location.

Now, when a changes value (and because the String Object is immutable) a new value is created now in memory and now a points in memory_2.

But hey, b still points in memory_1.

PS: Immutability is:

In object-oriented and functional programming, an immutable object is an object whose state cannot be modified after it is created.

3 Comments

Is it same for other objects also?
In java most Objects are not immutable. Examples of immutable objects from the JDK include String and Integer. (not int which is a primitive data type but Object Integer )
The behaviour here has nothing to do with immutability. It's a complete red herring. When a changes value, it changes value. It refers to a different object. There's nothing string-specific about that.
1
String a = test

So a is an Object Reference Variable pointing to a String Literal "test" on the heap.

String b = a;

No b is an Object Reference Variable also pointing to the same String Literal "test" on the heap.

Now,

a = "wuut";

But b is still pointing to the String Literal "test",

so Its b which holds the reference to the Object which was previously also referred by a, and but Not to a.

1 Comment

@Ya thats what i mean...... cause its obvious that why i made it bold, still if its hard to read...let me edit it
0

No it doesn't. b assumes the value of a. If a changes later, b doesn't. It's not an alias or anything of that nature.

Comments

0
String a = "test";

Now a holds a reference to the string "test".

String b = a;

The value of a is copied to b. A is a reference to "test", so now b is a reference to "test" too.

a = "wuut";

Now a is assigned a reference to "wuut". This doesn't affect b, because b doesn't hold a reference to a.

Comments

0

No. b holds the value of a which is a reference to the string "test". What you're thinking about is if you modified the string that a pointed to that b would also change (something like a.append("wuut");). However it is not possible to change Java string because they are immutable.

Comments

0

Let's go through this step by step.

  1. String a = "test" - create object "test" and reference a point on this object.
  2. String b = a - create new reference b point to the same object "test"
  3. a = "wuut" - reassign reference a to another object, but reference b still point to object test

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.