*
The moose likes Beginning Java and the fly likes Increment operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Increment operator" Watch "Increment operator" New topic
Author

Increment operator

Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

int method1(int x){
return x;
x=x+1; //generates a compiler error. Unreachable okay. Doing something
after return statement not permitted. Fine.
}

int method1(int x){
return x++; //doesn't generate a compiler error. After returning x, value
is incremented . (Does it really increment? or the
JVM doesn't bother to do it.)

}

Please Clarify.
[ August 09, 2005: Message edited by: Arun Kumarr ]

If you are not laughing at yourself, then you just didn't get the joke.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Originally posted by Arun Kumarr:
Somebody clarify!!!

That's not a very nice way to ask someone to help you.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Sherry Jacob
Ranch Hand

Joined: Jun 29, 2005
Posts: 128
Originally posted by Arun Kumarr:
int method1(int x){
return x++; //doesn't generate a compiler error. After returning x, value
is incremented . (Does it really increment? or the
compiler doesn't bother to do it.)

}



Hi Arun,
Well, the syntax of the return statement is return(<expression>

<expression> can be any valid arithmetic expression which should evaluate to a value.

x++ is a valid arithmetic expression. Here in return(x++), the operation of incrementing and assignment are being performed in one single step.

Of course the compiler increments the value of x, but x++ means that the old value of x is first returned and then incremented.

That's why you get the same result and do not see the incremented value.

Hope this helps.


<strong><br />Cheers !!<br /> <br />Sherry<br /></strong><br />[SCJP 1.4]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Sherry Jacob:
Of course the compiler increments the value of x, but x++ means that the old value of x is first returned and then incremented.


To be exact, it means that the old value of x is remembered, than x is incremented and *finally* the old value is returned. So the incrementation really takes place! (The JVM might optimize it away, though, I suppose.)

To reiterate, because that's a common misunderstanding: the incrementation isn't delayed at all - it takes place exactly when the expression is evaluated. It's just that the value of the expression is the value of x before the incrementation.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

Hi Marilyn,
I'm sorry for that.
But to be frank,
I don't find any reason to be cautioned about "Somebody clarify".
Anyway I'll try to rephrase it next time.


Sherry,

I understand that the variable x is incremented.
But once JVM encounters a return statement then it is not supposed to do anything inside the method. Is my understanding correct?
Correct me if Iam wrong.
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

Thanks Ilja. That clarifies it.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
There is a big difference in English between
"Somebody clarify!!!" -- a shouted command/demand
and
"Please Clarify." -- a polite request for someone to volunteer to help you.

It's all part of being nice.

By the way, Ilja's explanation is correct. The statement is evaluated. The incrementation is part of the evaluation and the result is put into a temporary place. Then the original value of 'x' is returned. You can see for yourself if you use the javap -c command.
[ August 09, 2005: Message edited by: Marilyn de Queiroz ]
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6



But once JVM encounters a return statement then it is not supposed to do anything inside the method. Is my understanding correct?
Correct me if Iam wrong.


When there's return <foo>; then first <foo> is evaluated, and its results are returned.

Doesn't matter if it's

In all cases, the expression on the right is evaluated (which may involve executing a method or an increment) and THEN the result of that expression is returned.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

And not to confuse the issue, but in the interest of completeness, if the return statement is inside a try or catch that has an associated finally, then the body of that finally is executed before control returns to the caller.

You'll see "finally".
 
 
subject: Increment operator
 
Similar Threads
for(? ; ? ; ?)
Method Declaration confusion!!!
Unreachable Code
Return Keyword Help!
int divided by int