Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Really...an unexpected answer(Increment)

 
Karthikeyan Balasubramanian
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
inc.fermin(i);
i=i++;
System.out.println(i);
}
void fermin(int i){
i++;
}
}
 
Shalini Chandel
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

regards,
Shalini
 
Karthikeyan Balasubramanian
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And of course, the fermin() method doesn't change i because it is passed by value.
 
Piyush Sam
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.

Regards,
[ June 22, 2005: Message edited by: Piyush Sam ]
 
tarun navlani
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just chill out guys

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

1 ) Checks the precedence of operator in the experssion

i=i++;

++ (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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic