• 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

associativity and precedence...prob..pls..help

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Can any1 explain 2 me how this follwing e.g. prints 0 and 3 resp.
I know the 1st 1 is heppening due to lowest precedence. But,if anyone can explain with any other e.g....I think that will be better for me.In addition...pls. explain abt. precedence order.
THANKS IN ADV.
//The e.g.

public class Precedence {
final public static void main(String args[]) {
int i = 0;
i = i++;
i = i++;
i = i++;
System.out.println(i); // prints 0, since = operator has the lowest precedence !!!

int array[] = new int[5];
int index = 0;
array[index] = index = 3; // 1st element get assigned to 3, not the 4th element ???
for (int c = 0; c < array.length; c++)
System.out.println(array[c]);
System.out.println("index is " + index); // prints 3
}
}
<marquee> Ratul Banerjee </marquee>
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Ratul!
I am suprised from your code. How can be the the output be zero of i . How operatoe precedence is related to it. please explain.Thanks in advance
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<pre>
OPERATOR PRECEDENCE
P-ost-fix Operators [] . (parameters) expression++ expresion--
P-efix Unary Operators ++expression --expression
O-bject creation and cast new (type)
M-ultiplication * / %
A-ddition + -
S-hift << >> >>>
R-elational Operators < <= > >= instanceof
E-quality Operators == !=
B-itwise/boolean AND &
-itwise/boolean XOR ^
-itwise/boolean OR |
L-ogical AND &&
-ogical OR | |
C-onditional Operator ?:
A-ssignment = += -= *= /= %= <<= >>= >>>= &= ^= !=
</pre>
Let me first explain the 2nd example.

According to our table above, operator [] has higher precedence over assignment, therefore it gets evaluated first as [0]. The expression becomes array[0]=index=3. The next evaluation is for the two = operators. This follows the right to left associativity rule for = operator so, index gets assigned a value of 3, and in turn array[0] gets assigned the new value of index w/c is 3.

In the above example, the postfix operation (i++) gets evaluated before assignment (=) in accordance with our precedence table. If you were to translate the expression, i = i++, in english it would say, "evaluate value of i and use it in the expression before incrementing". So, i is evaluated as 0, then using it in the expression with right to left associativity, i=0. The increment ++ is evaluated but assignment has already happened so this "side-effect" is not utilized. Hence the output prints 0.
I have devised an acronym to help me remember operator precedence:
P-entium P-Cs O-utnumber M-ainframes A-lso S-ame R-eason E-very B-ug L-ikely C-omes A-lways
Hope this helps.

[This message has been edited by James Baud (edited March 24, 2001).]
 
shabbir zakir
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everybody!
Thanks James for your explanation. But
int i = 0;
i = i++(ie it becomes 0(1))
i = i++(ie it becomes 1(2))
i = i++(ie it becomes 2(3))
System.out.println(i) [ it should print 3]

Please help me understand this concept. Thanks in advance
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i=i++ pos(1) ;
causes the value of i to become 1 at pos(a) but the assignment causes 0 to be again assigned to i(due to post fix increment)
hope u understand
------------------
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please try your code by replacing
i=i++;
i=i++;
i=i++;
by
i++;
i++;
i++;
can u find some difference.
 
ratul banji
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Wht. sort of diff. u r talking abt....tvs sundaram
Pls..e x p l a i n.
<marquee> Ratul Banerjee </marquee>
 
ratul banji
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Thanks all of u.
Thanks James for ur explanation.
Ur own define method for remembering purpose is pretty good one.
Can u tell me another thing...how precedence and associativity differs...I mean 2 say...when should we look for precedence and when should we look for associativity.
Thanks all ..1ce again.
Regds.
<marquee> Ratul Banerjee </marquee>

 
ratul banji
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I think I have understood the 1st one.I think if we change our code like this:
int i = 0;
int x = 1;
int y = 2;
int z = 3;
x = i++;
y = i++;
z = i++;
System.out.println(x);// should print 1
System.out.println(y); // print 2
System.out.println(z); // print 3
System.out.println(i); // Now i is 3
In the erlier case its remain on 0 ...b'couse We were assignning back to i...
Am I right this time...pls..Correct me if I am wrong ..

<marquee> Ratul Banerjee </marquee>

 
James Baud
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ratul,
Your best bet would be to try out (compile & run) your code as you'll learn a lot this way.
Using your 2nd example, array[index] = index = 3;;
Operator [] has higher precedence than assignment =. Precedence lets you determine w/c one to apply first if you have operators of different precedence in an expression.
If two operators have the same precedence, associativity rules are used to determine w/c operator should be applied first. In your 2nd example again, we have two = operators that obviously have the same precedence so we need to apply the associativity rule for operator =, w/c is right to left.
Associativity rule:
Unary postfix and all binary operators associate from left to right, except for the assignment operator. All else associate from right to left.
------------------
~James Baud
He who asks, is a fool for five minutes;
but, he who does not ask, remains a fool forever. (Chinese proverb)
 
tvs sundaram
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai Rathul,
When u declare
i=0;
and
step1i=i++;
i=i++;
i=i++;
the value of i never gets incremented. This is because u try to assign a post increment operator value back to the variable.

Now in step 1
i=0++;
i.e., i=0;// the incremented value of i is not available.
same is the case in further steps. Hence when u try to print it after three steps u get 0 as o/p.
Now let's take the code i had given in my earlier post.
i=0;
i++;
i++;
i++;
Here u r not assigning the value of i back to i.
This is the important difference.
The value of i gets incremented in its memory position and the output after 3 steps will be 3.
Try this code
class postincretest
{
public static void main(String args[])
{
int i=0;
System.out.println(i);
i=i++;
System.out.println(i);
i=i++;
System.out.println(i);
i=i++;
System.out.println(i);

int j=0;
System.out.println(j);
j++;
System.out.println(j);
j++;
System.out.println(j);
j++;
System.out.println(j);
}
}
 
ratul banji
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks tvs.
Ur example is a good one.I have tried it.
Regds.
<marquee> Ratul Banerjee </marquee>

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi mr james baud
its ok, ur concept in java is ok.
But I think the same Precedence rules r followed by 'c' and 'c++', but if I do
i=i++;
in 'c' or 'c++', it works just fine and i gets the value 1, in case i'm trying in 'c' or 'c++', after its(i=i++) execution.
thanx if u can explain this.
anyway thanx a lot
Sameer

[This message has been edited by Sameer Sachdeva (edited March 30, 2001).]
 
ratul banji
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes...the concept b/w c++ and java is different in this particuler point.
Thanks.
Ratul
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Let me try my two cents worth.
First, the reason the (x = x++) does not change the original value of x is not becuase x was originally initialized with a '0'. This can be proven by initializing x with any integer value, such as 4, the value of x after the (x = x++) statement will still be 4.
Second, the compiler does ignore the increment operation just because the RHS and the LHS use the same variable. This can be proven that if we let x = y++, where y is another integer, the value of y will be increased by 1; meanwhile x would retain the old value of y.
So one would be confused to wonder "if the post-increment happen to y, why does it not happen to x " as one might think that after the post-increment x would have advance regardless of what was assign to x via = operator.
here is the answer:
Since the operation requires that the increment operation is to be postponed, the value of current x, let's say 4, is moved to a register first. Then post-increment takes place, x is now 5. The evaluation of the expression, which is in the register is now moved to x via the = operator, 5 is now back to 4.
Hope that's help.
Lam
 
ratul banji
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
T H A N K S ALL
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

in 'c' or 'c++', it works just fine and i gets the value 1, in case i'm trying in 'c' or 'c++', after its(i=i++) execution

In fact, the behavior is undefined according to the rules of c/c++. On some compilers it may increment and on others it may not. (My own experience is that in most cases, but not all, it increments). Java is a vast improvement because the behavior is defined and will not vary between JVMs.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what happens here: i think
i=i++;
JVM does following in background
1st step: int temp=i;
2nd step: i=i+1;
3rd step: i=temp;
let me know if i am wrong
Rick
[This message has been edited by Rick Zhong (edited April 13, 2001).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic