File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes method within method math Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "method within method math" Watch "method within method math" New topic

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(i++)
+ m(-i)
+ m(i++));

The answer in the sample exam, and in Eclipse, is:


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,

Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865

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="" target="_blank" rel="nofollow">Try my mock exam.</a>
Jerry Bustamente
Ranch Hand

Joined: May 24, 2004
Posts: 90
Thanks alot Dan!


I agree. Here's the link:
subject: method within method math
It's not a secret anymore!