| Author |
method within method math
|
Jerry Bustamente
Ranch Hand
Joined: May 24, 2004
Posts: 90
|
|
Sorry for all the recent questions. I am scheduled to take the test August 9, 2004 and am trying to nail down some things. I greatly appreciate all the help I've received. My latest challenge is the following: public class Exam1Q28 { static int m(int i) { System.out.print(i + ","); return i; } public static void main(String[] args) { int i = 1; m(m(++i) + m(i++) + m(-i) + m(i++)); } } The answer in the sample exam, and in Eclipse, is: 2,2,-3,3,4, I am confused as to how the -3 is transformed into a positive 3. I've debugged this in Eclipse and am just not understanding something. I step through it and after the -3 is printed then next step changes that value to 3. I set breakpoints at each line that will take one. Thanks in advance. Confused in Madison Wisconsin, JerryB
|
 |
Dan Chisholm
Ranch Hand
Joined: Jul 02, 2002
Posts: 1865
|
|
Jerry, The -3 is never transformed to a positive 3, because the value of variable i is never -3. Instead, the -3 is only the value of the argument passed to method m in the method invocation expression m(-i). The actual value of variable i remains positive 3 after the execution of the method, but the value printed as a result of the method invocation is that of the argument, -3. Just remember that the preincrement and postincrement instructions have side effects that change the value of the variable. The unary minus operator, -, does not have any side effects that change the value of the variable.
|
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
|
 |
Jerry Bustamente
Ranch Hand
Joined: May 24, 2004
Posts: 90
|
|
Thanks alot Dan! :-) JerryB
|
 |
 |
|
|
subject: method within method math
|
|
|