This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Basic doubt  RSS feed

 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println("Hi all");
I have a basic doubt.
int c = 10;
c = c++;
System.out.println(c);
Here c prints the value 10, instead of 11. I know that, in this case ++ is a postfix operator. But once the expression is evaluated, it should complete the postfix operation and thus should have the value 11. This is what I was thinking.
Thanks,
System.exit(0);
 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The confusion happens because you assigned it back to c again.
better way & for it to work as you expected use c++;

Originally posted by Jmannu gundawar:
System.out.println("Hi all");
I have a basic doubt.
int c = 10;
c = c++;
System.out.println(c);
Here c prints the value 10, instead of 11. I know that, in this case ++ is a postfix operator. But once the expression is evaluated, it should complete the postfix operation and thus should have the value 11. This is what I was thinking.
Thanks,
System.exit(0);

 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
c = c++;
Rule: Evaluate the operands before performing the operation. We must evaluate c and c++ before performing the assignment =.
1. Evaluate the left operand.
The result of the left operand is the variable c.
2. Evaluate the right operand.
Perform the postfix operator ++. Add 1 to the value of c and store it in c. Now c holds the value 11.
The result of the postfix expression is value of the variable before the new value was stored. The result of the expression c++ is 10.
3. Perform the assignment operation =.
Store the result of the postfix expression in c. Store 10 in c.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Notice the difference between the value of an expression and a side effect that may occur when an expression is evaluated:
Assume int c = 10; boolean b = false;
1. Here is an expression: c + 1
The result of the expression is 11. There is no side effect.
2. Here is another expression: c++
The result of the expression is 10. There is also a side effect.
The side effect is that 1 is added to 10 and stored in c.
3. Here is another expression inside the parentheses: if (b = true) {}
The result of the expression is true. There is also a side effect.
The side effect is that b is assigned the value true.
 
Manoj Gundawar
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Merelene,
Thanks for the detail explanation. Unfortunately I am not fully convinced. Although your try was really good.
Here is my doubt.

from the steps you have written:
2. Evaluate the right operand.
Perform the postfix operator ++. Add 1 to the value of c and store it in c. Now c holds the value 11.
The result of the postfix expression is value of the variable before the new value was stored. The result of the expression c++ is 10.
3. Perform the assignment operation =.
Store the result of the postfix expression in c. Store 10 in c.
====> here the assignment happens first and c gets value 10. But due to side effect c's value also get incremented at the end of the entire expression and gets stored in c in the same was as c++.
When next line is executed, the prior line is evaluated completely. So when println is executed, the value of C should be 11, as per your own explanation of the side effect in second example.
In this case where is that side effect happening?
I am still doubtfull about this.
ANyway...
Thanks for your reply.
Mannu
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jmannu
I guess you misunderstood Marlene's post.



here the assignment happens first and c gets value 10. But due to side effect c's value also get incremented at the end of the entire expression and gets stored in c in the same was as c++.




2. Evaluate the right operand.
Perform the postfix operator ++. Add 1 to the value of c and store it in c. Now c holds the value 11.


This means that you need to evaluate c++. So now c is 11.


The result of the postfix expression is value of the variable before the new value was stored. The result of the expression c++ is 10.


This means that when you do c++ on c which intially contains 10 then c would be incremented but the value returned by c++ will be the intial value of c. Example System.out.println(c++ + c++ + c++) the result would be (10 + 11 + 12) because the intial c++ causes c to become 11 and returns 10 so the equation's result is 33.

3. Perform the assignment operation =.
Store the result of the postfix expression in c. Store 10 in c.


The = operator has the last precedence. So the assignment of the value 10 will happen at the end when c has been incremented.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Anupam. That's it. Thank you.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an experiment. Sometimes code is better than words. It�s real.

Method int m(int)
0 iload_1
1 iinc 1 1
4 istore_1
5 iload_1
6 ireturn
iload_1 : The value (10) of the local variable at index 1 is pushed onto the operand stack.
iinc 1 1 : The local variable at index 1 is incremented by 1 (c = 11).
istore_1 : The value (10) on top of the operand stack is popped from the operand stack and the value of the local variable at index 1 is set to the value (c = 10).
Oh dear, out of scope again.
 
Manoj Gundawar
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Merelene, Anupam,
Now it's pretty clear to me. Sorry for being so dumb to not understand the earlier explanation
But example and the further details made it very clear. Now I have no doubts.
I appreciate it a lot. You people are making this place a better platform to understand Java.

Manoj
[ June 30, 2003: Message edited by: Jmannu gundawar ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!