aspose file tools*
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
Author

Explanation on the order of the printed values

Willem De Bruyn
Greenhorn

Joined: Nov 06, 2012
Posts: 5
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
Ranch Hand

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

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!


(...)
 
Don't get me started about those stupid light bulbs.
 
subject: Explanation on the order of the printed values