0

I have a question. I have the next sample of code :

class Test{
    static void change(String s){
        s = "newString";
    }
    public static void main(String args[]){
        String s = "String";
        change(s);
        System.out.print(s);
    }
}

I see that the result is "String". Now i have the following code :

class A{
    int a;
    static void change(A a){
       a.a = 10;
    }   
    public static void main(String[] args){
        A a = new A();
        a.a = 5;
        change(a);
        System.out.print(a.a);
    }
}

This ends up with the value 10. I cannot understand why? Aren't they both references? Why isn't the first code output : "newString"?

6 Answers6

4

The difference here is that in the first example:

static void change(String s){
    s = "newString";
}

you reassign the local variable s to point to a new String. This has no effect on the String that s previously pointed to, and has no effect outside of the method change().

In the second case

static void change(A a){
   a.a = 10;
}  

you are modifying the instance of A that the reference a is pointing to, therefore this change is still there when you return from the method.

Keppil
  • 45,603
  • 8
  • 97
  • 119
0

Java passes object references by value so you cannot reaffect the object that is passed as a parameter. This is what you see in your first example.

Yet you can of course modify the content of the passed object. This is what you see in your second example.

Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
0

In the String case, the reference s is a local copy so changing it does nothing.

In the int cases, you are using a reference to an A which holds another field. When you change that field, you are not changing a copy.

The equivalent is to do something like a = new A(); in the method which also would not change the original.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

In the first case, the method change(String s) is modifying a copy of the original reference.

In the second case, the method change(A a) is modifying a variable pointed by a reference, it's not the same. The reference a used in the method is a copy, but not the variable a.a that it points to.

Vincent Cantin
  • 16,192
  • 2
  • 35
  • 57
0

Java is Pass by Value

So in the first case when you get s in change, you are not getting the actual reference but you are getting a copy of reference and so assigning that a new value does not affect the old value.

In second case you are passing an instance of A, in which case you get a copy of reference a but you have a handle to actual values inside that instance. As a that holds an int a, when you change the value of int a it affects the actual value.

To clarify more, in second case if you do

change(A a){
   a = new A();
   a.a = 100;
}

it won't change the original a you passed to change from main()

Community
  • 1
  • 1
Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
0

Java is pass-by-value not pass-by-reference. It means that in method

void changeVer1(Something val){
    val = new Something("else");
}

val is local variable of changeVer1 method which is copy of reference you used as method's argument. It means that when you do

val = new Something("Else");

you are changing value of this local copy, so this will not change value of "original" reference you used as argument of this method.


Now lets take a look at other version of this method

void changeVer2(Something val){
    val.someField = 42;
}

Since val is copy of reference you passed it will hold same object as original reference and each change on state of this object will be visible via original reference.

Pshemo
  • 122,468
  • 25
  • 185
  • 269