File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Associate Certification (OCAJP 7/8) and the fly likes Explanation on the order of the printed values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Associate Certification (OCAJP 7/8)
Bookmark "Explanation on the order of the printed values" Watch "Explanation on the order of the printed values" New topic

Explanation on the order of the printed values

Willem De Bruyn

Joined: Nov 06, 2012
Posts: 14
Hey, so here's the piece of code, with the output below.


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
Ranch Hand

Joined: Mar 08, 2009
Posts: 966
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

Joined: Apr 10, 2013
Posts: 32


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!

I agree. Here's the link:
subject: Explanation on the order of the printed values
It's not a secret anymore!