jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes Compound method explanation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Compound method explanation" Watch "Compound method explanation" New topic

Compound method explanation

cornel jew
Ranch Hand

Joined: Jul 13, 2002
Posts: 37
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

Joined: Dec 10, 2001
Posts: 7023
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?

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link: http://aspose.com/file-tools
subject: Compound method explanation
jQuery in Action, 3rd edition