1

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); } } 
2
  • 1
    Because arguments are passed by value, not by reference. See also en.wikipedia.org/wiki/Evaluation_strategy Commented Feb 20, 2012 at 11:51
  • @FelixKling how can i solve this problem(update s) Commented Feb 20, 2012 at 11:55

7 Answers 7

6

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:

  • You can go and paint my front door red, which is making a change to the object. I will see that change next time I look at my house.
  • You can change the address written on your piece of paper.

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.

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

Comments

2

s is a local variable in getVal() and it does change - but only in the scope of getVal().

The value of s is passed to getVal() by value - thus only the copy is changed and not the original.

Comments

2

You pass the value s which has value 0; Then because it is passed by value, a new variable is created internally in getVal which initializes to 0. It gets the value 2 assigned. Then the variable is removed with the ending of the function.

Comments

1

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); } } 

Comments

1

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.

Comments

1

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); } } 

2 Comments

i have update too many arguments. return is not solve my problem.
@spk: You haven't presented your problem - you've asked why one bit of code works the way it does. Many answers have explained why. Perhaps if you'd asked a question relating to what you were trying to achieve, you'd be further along...
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.