0

I've just started learning Java and I had a question about some practice code I was looking at. Essentially, I'm unable to understand why our output in this case is 7 14 as opposed to 14 14. The code is the following:

class Test {
 static int s;
 public static void main (String [] args)
 {
     Test p = new Test();
     p.start();
     System.out.println(s);
 }

 void start()
 {
     int x = 7;
     twice(x);
     System.out.print (x + " ");

 }

 void twice (int x)
 {
     x = x* 2;
     s = x;
 }
}
David W
  • 17
  • 5

6 Answers6

4

Perhaps it would be more clear if we re-wrote the twice method as

void twice (int y)
{
     y = y * 2;
     s = y;
}

The x in start is completely unrelated to the x in twice.

tskuzzy
  • 35,812
  • 14
  • 73
  • 140
1

Because you print x here,

twice(x);
System.out.print (x + " "); // <-- x

And the value of x that was modified in twice is not the same as the caller's x. So x is still 7.

If you did,

System.out.print (s + " "); // <-- not x

you would get the output you expected.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

In java, when you pass in a primitive data type, the actual reference is not passed in; a copy of the value is passed in, which is independent of the original value's reference.

So: x is 7 locally in the start() method. When the twice() method is run, a copy of x(with a value of 7) is locally made, and then doubled. The static variable s is then assigned the value(fourteen). Finally, x(in start) is printed and then the static value is printed, which has been given the value 7.

kirbyquerby
  • 735
  • 5
  • 16
0

since the scope of the parameter in the function twice (int x) is within it function. the values assigned will not be reflect globally. It will send the parameters as the copy. This way is called pass by value.

so you have to assign it to some global variable to get back the result or make the function to return the result by changing return type.

Sridhar DD
  • 1,972
  • 1
  • 10
  • 17
0

Every time you write int x, you get a new variable. So you got one variable when you wrote int x = 7; inside the start method. Then you got a second variable when you wrote (int x) at the top of the twice method. The fact that they have the same name is irrelevant - these are two different variables.

Now when you wrote twice(x) inside the start method, that copied the value 7 from the first x variable to the second x variable. After that, any changes you made to the second x are not going to affect the first one.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
0

Try a int returning method that modifies x instead of a void method that just affects the s variable, like this:

class Test {
 static int s;
 public static void main (String [] args)
 {
     Test p = new Test();
     p.start();
     System.out.println(s);
 }

 void start()
 {
     int x = 7;
     x = twice(x);
     System.out.print (x + " ");

 }

 int twice (int value)
 {
     int result = value* 2;
     s = result;
     return result;
 }
}

Since java is pass-by-value for primitives (such as int, boolean, etc) and pass-by-reference for Objects you must return something, take a look at this blog that explains some more about that

Diego López
  • 1,559
  • 1
  • 19
  • 27