• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Explanation on the order of the printed values

 
Willem De Bruyn
Greenhorn
Posts: 14
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, so here's the piece of code, with the output below.

OUTPUT:
20
10
11
10


I can't wrap my head around it, I can see why the 20 and the 11 should print, but why the 10 gets printed twice I don't get.
 
Piet Souris
Rancher
Pie
Posts: 1323
29
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey Wikkelzzz,

I hope I don't wake you up, but:

1) use codetags!

2) the arguments to both methods are copies of ejg. These copies get changed, but ejg itself is not.
 
Alex Derek
Ranch Hand
Posts: 32
Mac OS X Safari Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

i'll try to help you. :-) (i'm also preparing this exam).

On line 04, anotherMethod(ejg), prints 20, because, inside the method on line 18 there is a System.out.println(val).
When you call the method, you pass the variable ejg as parameter of the method, and so val has the value of ejg, 10.
This value get changed to 20 on the first line of the method (line 17).
Then the method prints this value on line 18. Then compiler go back to line 05 and prints the value of ejg, which has not
been changed and it's still 10.

Same thing for line 07 and 08.

good luck for the exam!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic