5

I have to get something straight. Please tell me if I'm correct:

String a;
String b;

a = b means that b takes the value of a?

And

b = a means that a takes the value of b?

I'm awkwardly confused about this & I'd appreciate an explanation.

Dake
  • 289
  • 7
  • 17
  • ah...it's the other way round.If the value of b is 5 and then you do a=b, the value of a is also now 5 – Rakesh G R Aug 19 '16 at 16:25
  • In Java, does `a=b` mean that a gets the *value* of b, or a gets a pointer to the *same object* as b? – Jonathan M Aug 19 '16 at 16:27
  • 2
    Read the symbol `=` as `becomes` – Kon Aug 19 '16 at 16:28
  • @Kon Oh. That's a nice way to put it. Thanks! – Dake Aug 19 '16 at 16:29
  • @JonathanM It depends on whether a and b are primitives (boolean, int, etc) or reference types. But either way, a is getting the value of b, really -- it's just that int he case of reference types, that value is the reference. See http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – yshavit Aug 19 '16 at 16:31
  • @yshavit It doesn't depend on whether it is primitive or reference. `a` gets the value of `b`, period. http://javadude.com/articles/passbyvalue.htm – Jaroslaw Pawlak Aug 19 '16 at 16:35
  • @JaroslawPawlak Did you read past my first sentence? :) – yshavit Aug 19 '16 at 16:36
  • @yshavit Yes, but that's the first sentence which shouldn't be there ;) – Jaroslaw Pawlak Aug 19 '16 at 16:38
  • @JaroslawPawlak Well I think we can both agree that (1) for primitives, a gets the value of b, and (2) for primitives, a does _not_ get a pointer to the same object as b (since no objects are involved). So the answer to Jonathan M's full question, including the second portion about the pointer, is "it depends." :) – yshavit Aug 19 '16 at 16:44

2 Answers2

9

Generally, a = b means "a becomes b". However, this is only part of the story when we talk about Objects (as opposed to primitives). After the assignment both variables or fields reference the same object. When object allows changes (which String does not) any changes you perform on a take an effect on b as well, and vice versa.

This can be illustrated with a simple diagram:

Assignment before and after.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
6

In Java, String a; declaration meaning , a is a reference variable of type String object in memory.

In your case, both a and b are String reference variables.

1) a = b , means a gets the reference of b. if b has any value, it will be assigned to a.

2) b = a , means b gets the reference of a. if a has any value, it will be assigned to b.

It can be easily tested with simple java program if you have started learning in Java.

Java Example:

package test;

    public class TestString {
        public static void main(String args[]) {

            String a = "Stack";
            String b = "Overflow";
            a = b;
            System.out.println("after [a=b]:" + "a =" + a + ";b=" + b);
            //after this a and b has Overflow as value.
            b = a;
            System.out.println("after [b=a]:" + "a =" + a + ";b=" + b);

        }
    }

output:

after [a=b]:a =Overflow;b=Overflow
after [b=a]:a =Overflow;b=Overflow
esha
  • 91
  • 8