Why the variable s is not changed by 2
public class Test { static void getVal(int s) { s= 2; } public static void main(String arg[]) { int s = 0; getVal(s); System.out.println(s); } } Why the variable s is not changed by 2
public class Test { static void getVal(int s) { s= 2; } public static void main(String arg[]) { int s = 0; getVal(s); System.out.println(s); } } As Felix says in the comment, in Java arguments are passed by value.
It's clearer if we use different variable names:
public static void main(String arg[]) { int x = 0; method(x); System.out.println(x); // Prints 0 } static void method(int y) { y = 2; } When you call method(x), the value of x is evaluated, and that value becomes the initial value of y. That's the only connection between the two variables. Changing the value of y doesn't change the value of x.
Now with reference type variables it may appear as if that's not the case:
public static void main(String arg[]) { StringBuilder x = new StringBuilder(); method(x); System.out.println(x); // Prints "appended" } static void method(StringBuilder y) { y.append("appended"); } It looks a bit like you've changed the value of x - but you really haven't. The values of x and y are just references to an object... both x and y refer to the same object, and the append call has changed the data within the object. The analogy I like to use here is with houses: as the method caller, I can give you a piece of paper with my address written on it. That address is the equivalent of the reference, whereas my house is the object.
You can change two things:
Neither of these makes a change to "where I live" - my copy of the reference to "my house" is the same as it was before, whatever you've done.
The behaviour when it comes to classes leads to a commonly-stated myth that "Java passes primitives by value and objects by reference" - that's simply untrue. Java passes all arguments by value - but you need to understand that the value of expression is always either a primitive value or a reference, never an object.
Argument are passed by value. In Method getVal, s is another variable generated. The Changes have no effect on the other variable s. To have this effect you can do like:
public class Test { static int getVal(int s) { s= 2; return s; } public static void main(String arg[]) { int s = 0; s = getVal(s); System.out.println(s); } } This is because of the fact that java passes values to methods as parameters. So When you call the function getVal(s) instead of s it passes value of s which is 0.
http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html
This document will tell you the story.
the argument is pass by value, so in getVal a new local variable is created. to slove this you should return the value and assign to variable in calling method. try this
public class Test{ static int getVal(int s){ s= 2; return s; } public static void main(String arg[]){ int s = 0; s = getVal(s); System.out.println(s); } } Java does manipulate objects by reference, and all object variables are references. However, Java doesn't pass method arguments by reference; it passes them by value.
Take the fun() method for example:
public void fun(int var1, int var2) { int temp = var1; var1 = var2; var2 = temp; } When fun() returns, the variables passed as arguments will still hold their original values. The method will also fail if we change the arguments type from int to Object, since Java passes object references by value as well.