• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Bowled over by a simple code

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because i++ return 0, since ++i = 1
Point to remember - position of ++ and -- operators is significant.
------------------
Alex J.Grig
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
jayram
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is because i++ will first assign the value stored in it i.e 0 & then it increments
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This piece of code might help...

Shyam
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//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
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Patrick Hoey
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dave and Patrick ...
Thanks a lot now its as clear as sunlight
Cheers
Jayram
------------------
What if this is as good as it gets ?
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ruth Stout was famous for gardening naked. Just like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic