Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compound method explanation

 
cornel jew
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I was doing one of Dan's mock exam on operators and came across the following code ,but could not really get the grip of the answers.
Can someone please explain how to get the results with samples please.
class C {
static int m(int i) {
System.out.print(i + ", ");
return i;
}
public static void main(String s[]) {
int i = 1;
m(m(++i) - m(i++) + ~m(-i) * -m(~i));
}
}
The answers of 2,2 i understand but -3,-4, 8 i don't understand
Thanks for any help
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In case it wasn't clear to others reading this thread, the output of this program is "2, 2, -3, -4, 8".
------------------------
First - If you ask me, what you should really understand from this example, is that it's very bad practice to program like this. Code should be written to be easily read and understood. Big, complex statements with compound assignments are not easily read and understood.
Second - cornel, what do you think the program should display and why?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic