• 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

Help me out

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi friends ,
This que . is from velumurugan's notes
public class hi {
public final static void main(String args[])
{
int i = 0 ;
i = i++;
i = i++;
i = i++;
System.out.println(i);
}
}
Output is 0 ;
can somebody tell me how ?
thanks in advance
rahul
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here's a good thread in java.sun.com
http://forum.java.sun.com/read/16789542/qAa6ifNJoxK4AAotL#LR
 
lee dalais
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if the thread doesn't come up, this is an explanation from a guy that posted one,
Hi everyone,
When was teaching Java, one of my students has e-mailed me several questions and one of the questions was the one that you are discussing here.
Here is my explanation. The following chart is only the portion of a complete chart.
Operator Precedence Chart:
=========================
Operators are shown in decreasing order of precedence from top to bottom.
Rank Operator Type Associativity
==== ======== ============= =============
1 () parentheses left to right
2 [] array subscript left to right
...
5 ++ uniray post-increment right to left
...
...
16 + arithmetic addition left to right
...
...
34 = assignment right to left
...
...
45 >>>=
so, the operator "=" has the lowest precedence in the following expression:
i = i++;
The expression that has to be evaluated prior to the assignment is: i++.
The post-increment means the value will be plugged in first prior to the expression evaluation.
So i = i++ can be viewed as two statements
i = 0 -- value is plugged in prior to increment
i++ -- post-increment expression
The post-increment has higher precedence than assignment. Due this reason, expression will be evaluated prior to the assignment. So "i" will be increated by 1. That means the value of "i" is 1.
Then the assignment will be performed as i = 0. That means the value of "i" is changed from 1 to 0.
The conclusion is that the variable "i" will be assigned to 1 during the expression evaluation but the value is temporary one. At the end (after assignment) the variable "i" will have 0.
Let us see the case with pre-increment.
i = ++i;
In the case of pre-increment the value will be plugged in after the increment. That means, "i" will be incremented to have a value of 1, and then value will be plugged in before the assignment.
i = 1;
In this the variable "i" will have a value of 1.
Hope this helps.
Thank you
Suresh Vuluvala
 
Yup, yup, yup. 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