Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Increment operator

 
Arun Kumarr
Ranch Hand
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Marilyn de Queiroz
Sheriff
Posts: 9066
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arun Kumarr:
Somebody clarify!!!

That's not a very nice way to ask someone to help you.
 
Sherry Jacob
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Arun Kumarr
Ranch Hand
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ilja. That clarifies it.
 
Marilyn de Queiroz
Sheriff
Posts: 9066
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic