aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Bowled over by a simple code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Bowled over by a simple code" Watch "Bowled over by a simple code" New topic
Author

Bowled over by a simple code

jayram
Ranch Hand

Joined: Oct 30, 2000
Posts: 94
Guyz
following code printed 0 as result i was expecting it to print 1...whatz happening here :O
{....
...
int i=0;
i=i++;
System.out.println(i);
....
}
So whatz happening??why is it printing 0???
Any clues??
------------------
What if this is as good as it gets ?
Alex Grig
Ranch Hand

Joined: Oct 19, 2001
Posts: 38
Because i++ return 0, since ++i = 1
Point to remember - position of ++ and -- operators is significant.
------------------
Alex J.Grig


Alex J.Grig
Jim Petersen
Ranch Hand

Joined: Jul 24, 2001
Posts: 241
Hi
As Alex says op++ Increments op by 1 but evaluates to the value of op after the original value of op has been used in the expression
rgds Jim


- Jim Petersen <br />SCJP2<br />SCWCD<p>- but then again, I could be wrong...
jayram
Ranch Hand

Joined: Oct 30, 2000
Posts: 94
Thanx for your reply alex. What you are saying is correct.
But what happened to i++ dear ??
i++ should increment the value of i by 1. If i++ is getting executed where is the incremented value ?

jay
Alex Grig
Ranch Hand

Joined: Oct 19, 2001
Posts: 38
What's the problem?
After i++ the i = 1 and i++ = 0. Now we assign i to i++ it means we assign i to 0. Correct
------------------
Alex J.Grig
Abdul Latif
Ranch Hand

Joined: Jul 18, 2001
Posts: 89
this is because i++ will first assign the value stored in it i.e 0 & then it increments
Matt Burba
Greenhorn

Joined: Oct 29, 2001
Posts: 8
Y'all aren't the first to ponder this topic. Here's a thread of messages from the Java Developer Connection in March: http://forum.java.sun.com/thread.jsp?forum=31&thread=54201
As a java newbie studying to take the SCJP test this Friday (11/2/01), I found the last answer (#10) in the thread to be the most understandable.
As an aside, this code illustrates horrible programming technique and I hope that Sun isn't going to be this picky on test day.
jayram
Ranch Hand

Joined: Oct 30, 2000
Posts: 94
well i guess sun was a bit picky ...some people have faced this question in SCJP exam ...
hmm i am still confused ..i guess i need to read that post with a hot cup of coffee
Cheers
Jayram
------------------
What if this is as good as it gets ?
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
This piece of code might help...

Shyam
Patrick Hoey
Greenhorn

Joined: Oct 11, 2001
Posts: 5
//Take a look at this code
//This should help you understand the subtlety
public class Test
{
public static void main(String[] args)
{
Test testVal = new Test();
testVal.test1();
testVal.test2();
testVal.test3();
}
public void test1()
{
int i=0;
i=i++;
System.out.println(i); //prints out 0 (assignment happens
//before increment
}
public void test2()
{
int i=0;
i++;
System.out.println(i); //prints out 1 (increments)
}

public void test3()
{
int i=0;
i=++i;
System.out.println(i);//prints out 1 (assignment happens
//after increment
}

}
/*
The issue here is how expressions are evaluated
In the Java Language Specification 2.0
Section 15 deals with Expressions
Specifically 15.14.1 and 15.15.1 are the sections
you want to read.
For this expression: ++i;
(Prefix Increment Operator)
"Value of prefix increment expression
is the value of the variable _AFTER_
the new value is stored"
From this, I would gather that the expression
"i=++i" would equal "(++(= i i))" where i is
incremented before the assignment.
For this expression: i++;
(Postfix Increment Operator)
"Value of the postfix increment expression
is the value of the variable _BEFORE_
the new value is stored"
From this, I would gather that the expression
"i=i++" would equal "((= i i)++)" where i is
incremented after the assignment already
took place. So i gets initialized as 0, and
in the expression, i=i++; i is assigned 0
before i is incremented.

*/
jayram
Ranch Hand

Joined: Oct 30, 2000
Posts: 94
ok patrick ...u almost cleared my doubt ...just one more clarification
when its written like i=i++ as u said value 0 will be first assigned to i on LHS. So 'i' on LHS is 0. Now i on RHS should get incremented before the control goes to next statement. Is that correct? If it is so then 'i' on RHS becomes 1 before printing anything to the output.I wont have any doubts in case of a statement like m=i++ no doubt m has to be 0 but in this case??? what happpened to '++'. Didnt it get executed at all?? or in memory 'i' on LHS is different that 'i' on RHS...anyways i would never write something like that in a real app
(i am reading JLS more carefully once more letz see if i can understand this thing better)
------------------
What if this is as good as it gets ?
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
joshi
From the JLS Section 15.14.1
The value of the postfix increment expression is the value of the variable before the new value is stored.

What happens is that the initial value of x is stored in a temporary register, then x is incremented, then the value stored in the register is asigned to the left hand side of the expression, in this case the LHS is x so x gets its original value.
int x = 1;
x = x++;
Steps:
1 initial value of x is stored in temp register. So temp = 1.
2 x is incremented. x = 2 and temp = 1.
3 the value of the temp register is assigned to the LHS. x = 1
hope that clears it up


------------------
Dave
Sun Certified Programmer for the Java� 2 Platform


Dave
Patrick Hoey
Greenhorn

Joined: Oct 11, 2001
Posts: 5
public void test4()
{
int i=0;
int m=0;
System.out.println("Beginning");
System.out.println(i); //prints 0
System.out.println(m=i++); //prints 0

System.out.println(m); //prints 0
System.out.println(i); //prints 1
System.out.println("End\n");
}
This shows the sublety here in this example. Before I stated that the postfix increment happens after the assignment. (expressions are evaluated from left to right).
Found this in the online tutorial at:
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arithmetic.html

" op++ : Increments op by 1; evaluates to the value of op before it was incremented

++op : Increments op by 1; evaluates to the value of op after it was incremented "
So in the expression i=i++;, i does increment, but gets evaluated to the value it was before it was incremented, which is 0.
0 gets assigned to the variable i (i=i++), which overwrites the original value of i which was 1. This is very tricky, but basically in how it gets evaluated, the value 1 gets overwritten with the value 0, due to how postfix notation evaluates the expression.
Hope this helps.
suma krishnan
Ranch Hand

Joined: Oct 19, 2001
Posts: 42
public class TestFor {
public static void main(String[] args) {
int x, y;
for (x=1,y=0; x < 5; x++, y++) (x++);
{ y++; }
System.out.println("The value of x is =" + x);
System.out.println("The value of y is =" + y);
}
}
This will print x=5
y=3
x=1 |y=0
x=x++=2 |y=y++=1
x=x++=3 |
x=3 |y=1
x=x++=4 |y=y++=2 after terminated
x=x++=5 |{y++}will take incremented to 3.
x<5 for loop will terminate now.

1.one thing we have know {y++} different statement this will take notice after the loop terminated.
2.(x++);will take within the for loop method count,note the difference.
correct me If I'm wrong
Fei Ng
Ranch Hand

Joined: Aug 26, 2000
Posts: 1242
Originally posted by joshi jayram:
well i guess sun was a bit picky ...some people have faced this question in SCJP exam ...
hmm i am still confused ..i guess i need to read that post with a hot cup of coffee
Cheers
Jayram

Yes.. i got a question just like this on the real exam.
jayram
Ranch Hand

Joined: Oct 30, 2000
Posts: 94
Dave and Patrick ...
Thanks a lot now its as clear as sunlight
Cheers
Jayram
------------------
What if this is as good as it gets ?
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Originally posted by Dave Vick:
joshi
From the JLS Section 15.14.1
The value of the postfix increment expression is the value of the variable before the new value is stored.

What happens is that the initial value of x is stored in a temporary register, then x is incremented, then the value stored in the register is asigned to the left hand side of the expression, in this case the LHS is x so x gets its original value.
int x = 1;
x = x++;
Steps:
1 initial value of x is stored in temp register. So temp = 1.
2 x is incremented. x = 2 and temp = 1.
3 the value of the temp register is assigned to the LHS. x = 1
hope that clears it up

Things can be done in this way also :
int x = 1;
x = x++;
Steps:
1 initial value of x is stored in temp register. So temp = 1.
2 temp is incremented. x = 1 and temp = 2.
3 RHS x is assigned to LHS x.
4 the value of the temp register is assigned to the RHS x = 2.
Can u define the need of temp variable. this thing can be done in less steps and and less memory.
Steps:
1. assign RHS to LHS.
2. increment RHS by 1.
required step is less and memory usage is less.
And then in this case it is not compiler dependant???

------------------
Regards
Ravish


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
 
 
subject: Bowled over by a simple code