jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Really...an unexpected answer(Increment) 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 "Really...an unexpected answer(Increment)" Watch "Really...an unexpected answer(Increment)" New topic

Really...an unexpected answer(Increment)

Karthikeyan Balasubramanian
Ranch Hand

Joined: Apr 20, 2005
Posts: 89
Hii everybody..
My preparation is going on for SCJP.
Below is the question....which gives the answer..0 (I EXPECTED 1)though we increments it and prints the variable...

wat's the new behaviour here ...for ++ operator.

thanks in advance.

public class Inc{
public static void main(String argv[]){
Inc inc = new Inc();
int i =0;
void fermin(int i){

SCJP 1.4--93%<br />SCWCD 1.4--86%<br />Next--???
Shalini Chandel
Ranch Hand

Joined: May 05, 2005
Posts: 115
You are using post-increment operator.

i=i++ first assign the value of i that is 1 in this case and then increment the value.

Here i actually assign 0 to i so it will print 0.

But if you use i+=1, this gives you answer as 1.


SCJP 1.4
Karthikeyan Balasubramanian
Ranch Hand

Joined: Apr 20, 2005
Posts: 89
Hi...i can understand wat u r saying.
if i=0 means
i=i++ .
here i=0 bot we r incrementing...right
i++ means i=i+1 right.
then i shuld print 1..howcome 0.
need some more explanation.
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

First, this is horrible code that should be refactored immediately and the original programmer slapped with a dead mackerel.

Second, what happens with that code is this (at least in theory - a smart compiler should optimize this out):

The value contained in the 'i' variable is incremented. However, the expression 'i++' evaluates to the pre-increment value of 'i'. The '=' assignment operator has the least precedence, so it happens last: the pre-increment value of 'i' is assigned back to 'i', effectively leaving you with the original value of 'i'.

The moral? Don't write silly code like this.
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
And of course, the fermin() method doesn't change i because it is passed by value.
Piyush Sam

Joined: Jun 02, 2005
Posts: 24

now try this code, your dout will be clear, j will print 0 and i will 1

and yor are right Timmy in ferlin() method i is paassed by value so any changes made inside this method will not have any effect outside of this method.

[ June 22, 2005: Message edited by: Piyush Sam ]

Piyush Jain<br /> <br />Being happy doesn't mean everything's perfect. It means you've decided to see beyond the imperfections.
tarun navlani

Joined: Jun 22, 2005
Posts: 1
just chill out guys

code written above is wierd
lets see wat the compile does...

1 ) Checks the precedence of operator in the experssion


++ (post increment) operator has lower precendce than = operator
so = (assignment) operation takes place first

2) i is initialized to 0
so 0 is assigned to i again

3) next operator in precendce is ++
but watch out the compile determines that i is on right hand side of the expression
++ (post increment) operation on i is ignored by the compile

so finally the output is still 0
I agree. Here's the link: http://aspose.com/file-tools
subject: Really...an unexpected answer(Increment)
It's not a secret anymore!