-1
public class Main {
    public static void main(String[] args) {
        int x = 0;
        int[] arr = {20};
        String name = "Vitalii";
        f(x, arr, Name);
        System.out.println(x + " " + arr[0] + " " + Name);
    }

    private static void f(int x, int[] arr, String Name){
        x = 20;
        arr[0] =40;
        name = "Max";
    }
}

The output I have is "0 40 Vitalii". I don't understand why in method f, jvm insert 40 at first position in array, but do nothing to string. Why it doesn't change it's value to "Max"? I'm sure I miss some important concept about “pass-by-reference” or “pass-by-value” or something like this. Thanks for helping.

azurefrog
  • 10,785
  • 7
  • 42
  • 56
  • The linked question has some answers which provide good explanations of what "pass-by-value" means in Java. [This answer](http://stackoverflow.com/a/12429953/1361506) in particular has a very good and graphical step-by-step explanation. – azurefrog Sep 21 '15 at 17:48

4 Answers4

1

Since local variable values are printing here. Java follows pass by value. After the method call values will not be changed.

int x = 0;
int[] arr = {20};
String name = "Vitalii";
f(x, arr, name);//here you are calling f() method
System.out.println(x + " " + arr[0] + " " + name);
SatyaTNV
  • 4,137
  • 3
  • 15
  • 31
  • "Java always follows pass by value". That's not actually true. Only primitives are pass-by-value, but arrays (even arrays of primitives) and objects are passed by reference. – neuronaut Sep 21 '15 at 17:50
  • 4
    But those references are passed by value. – SatyaTNV Sep 21 '15 at 17:51
  • No, they are only passed as a reference. Sure, beneath the hood the reference is just a number whose value is getting put onto the stack (rather than a reference to the reference or something), but it's misleading to people unfamiliar with Java to say that everything is passed by value in Java. Especially since there's no way to directly access the reference itself (only the values being referred to). – neuronaut Sep 21 '15 at 17:56
  • @neuronaut Actually it is true that everything in Java is passed by value. The value itself holds the reference. Unlike c++ where you can pass by reference by adding the ampersand symbol. – user3437460 Sep 23 '15 at 08:21
  • @user3437460 I suppose I mis-phrased things. In the sense that the original variable is just a reference (rather than the actual array/object) when that variable is passed in a call that reference is what's passed (rather than a reference to that reference), so I should have said "Java passes references" rather than "Java passes *by* reference". At any rate, I was trying to point out that this distinction is often lost on novice developers, so it's less confusing to just say "Java passes objects by reference and primitives by value" and let them learn the rest as they gain experience. – neuronaut Sep 24 '15 at 20:52
0

If you want to change the value of name do this:

public class Main {
    public static String name; 
    public static void main(String[] args) {
        int x = 0;
        int[] arr = {20};
        name = "Vitalii"; 
        //only classes should be capitalized
        name = f(x, arr, name);
        System.out.println(x + " " + arr[0] + " " + name);
    }

    private static String f(int x, int[] arr, String name){
        x = 20;
        arr[0] =40;
        String newName = "Max";
        return newName;
    }
}

Proof: https://ideone.com/tiT5YN

BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
0

Java passes everything by value, and this is especially true for primitive.

So in your method:

private static void f(int x, int[] arr, String Name){
    x = 20;
    arr[0] =40;
    name = "Max";
}

When you pass in x, the value of x was passed in and received by the local variable of method f. Hence any changes to it will not affect the original x.

x = 20;   //No changes to original x

String in Java in immutable. Which means once created, you cannot change it unless you create a new String.

name = "Max";  //No changes to name.

When you pass in the array, it is inefficient to make a copy of all array values and pass into a method, hence a value which holds the reference of the array was passed. In this case, you can directly affect the elements in the array, hence resulting a change when you do arr[0] = 40.

arr[0] =40;  //Changes to arr[0]
user3437460
  • 17,253
  • 15
  • 58
  • 106
0

Because Java supports pass by value. Hence instead of passing the variable reference it copies the value of the instance variable to the local variable. So any changes done to the local variable will not effect the actual/instance variable

Rahman
  • 3,755
  • 3
  • 26
  • 43