4

Can somebody tell me why the value returned is 3 and not 8. Doesn't the return x statement from the addFive method change the value of x in the main method?

public class App {
    public static void main(String[] args) {
        int x=3;
        addFive(x);
        System.out.println("x = " + x);
    }

    private static int addFive(int x) {
        x += 5;
        return x;
    }
}
Bobulous
  • 12,967
  • 4
  • 37
  • 68
flutter
  • 6,188
  • 9
  • 45
  • 78
  • You should see this related question: http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value?rq=1 – Bobulous Mar 19 '15 at 21:11
  • I think you should really accept an answer from the ones listed below, anyway I suggest to read Arkanon question and have full grasp of java always-pass-by-value thing – niceman Mar 19 '15 at 21:23

8 Answers8

10

You want x=addFive(x); rather than just addFive(x). Calling addFive(x) on it's own does not apply the returned value to any variable.

Jud
  • 1,324
  • 3
  • 24
  • 47
5

You have to set the returned value to a variable, otherwise it is lost and you are retrieving the value of "x" in your main method. Do this instead to capture the return value.

   public static void main(String[] args) {
       int x=3;
       x = addFive(x);
       System.out.println("x = " + x);
   }

If you only want to see the returned value and not store it, you can even put the function call inside the System.out.println.

   public static void main(String[] args) {
       int x=3;
       System.out.println("x = " + addFive(x));

   }
Evan Bechtol
  • 2,855
  • 2
  • 18
  • 36
3

Like everyone else is saying, you need to assign your return value. Because you're doing "addFive(x)" instead of "x=addFive(x);" you're just printing the instance of "x" in main, and not ever getting the value that your function returns.

This is because "x" in your main function is an instance variable, and your "x" in addFive() is a local variable. These are not the same variable, even if they have the same name. This might clarify a bit - http://www.tutorialspoint.com/java/java_variable_types.htm

adamclmns
  • 29
  • 3
2

when you call a method in java and you don't assign for any variable, the changes will happen in the method call, after that this value will be lost and go back to the value that you assign. to see the result you should do that System.out.println(addFive(x)); but if you want to change the value of x you have to assign x = addFive(x);

Joe Doe
  • 141
  • 1
  • 3
1

You are calling the method addFive(int x) with x, but not assigning the returned value to anything. So, inside main()'s scope x remains as before, 3 - which is what is being printed. So, you can either store the returned value to x itself:

x = addFive(x);

or make the function call within print statement:

System.out.println("x = " + addFive(x));
Sidmeister
  • 856
  • 2
  • 14
  • 27
1

The method does return a value, but you have to set the value to a variable too when you return it, or else how would it know which variable you want to return the value to? You can have 10 variable, and if you just call the method, how will it know which variable to return the number to? That's why you have to set the returning number it to a variable like this:

x = addFive(x);
Ski
  • 111
  • 2
  • 3
  • 17
0

Because you're simply not using the computation of your function. It doesn't change value of x, it returns new value.

You should do something like:

int y = addFive(x);
slnowak
  • 1,839
  • 3
  • 23
  • 37
0

The value in the main function is completely different from the value in the addFive(int x ) function. You send an x from main to addFive(int x) method.

JVM makes a copy of x and send it to addFive(int x) method. Then x changes in the addFive(int x) method. But the x in main() method remains unchanged.

If you want to get the changed value returned by addFive(int x) from main method you can do the following -

int returnedValueFromAddFive = addFive(x)  

Hope it will help.
Thanks a lot.

Mehmood Arbaz
  • 285
  • 1
  • 3
  • 11