• 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

Precedence and associativity of post and pre fix operators

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai
According to 'Programmer's guide to Java certification ' by Khalid A.Mughal (Page 42)

post-fix operator has high precedence than prefix operator and
pre-fix operator associativity is from right- left
But when I run the code given below
I found that both post-fix and pre-fix operator have same precedence and both have left to right associativity

{
int no ;
int ans;
no =1;
ans = ++no + 2*++no;
//associativity expected right-left
//prints 8 expected 7
System.out.println( " ++no + 2*++no = "+ans);

no =1;
ans = ++no*2 + ++no;
//associativity expected right-left
// prints 7 expected 8
System.out.println(" ++no*2 + ++no = "+ans );
no =1;
ans = ++no + no++ *2;
// precedence expected post-fix before prefix
//prints 6 expected 5
System.out.println(" ++no + no++ *2= "+ans );

no =1;
ans = ++no*2 + no++;
//check precedence expected post-fix before prefix
//prints 6 expected 7
System.out.println( " ++no*2 + no++ = "+ans );
}
'Java How to program' by Deital & Deital gives another view . It says (page 144) both have right to left associativity with post-fix having more precedence than prefix.
Can anyone tell me which one is right?
Thanks
Raji
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I will try and walk you through your examples:
no = 1;
ans = ++no + 2*++no;
Compiler does:
ans = (++no) + 2 * (++no)
ans = (1+1) + 2 * (++no) // no = 2 after this
ans = 2 + 2 * (++no)
ans = 2 + 2 * (2+1) // no = 3 after this
ans = 2 + 2 * 3
ans = 2 + 6
ans = 8
-----------------------------------------------
no = 1;
ans = ++no*2 + ++no;
Compiler does:
ans = (++no) * 2 + (++no)
ans = (1+1) * 2 + (++no) // no = 2 after this
ans = 2 * 2 + (++no)
ans = 2 * 2 + (2+1) // no = 3 after this
ans = 2 * 2 + 3
ans = 4 + 3
ans = 7
----------------------------------------
no = 1;
ans = ++no + no++ *2;
Compile does:
ans = (++no) + (no++) * 2
ans = (1+1) + (no++) * 2 // no = 2 after this
ans = 2 + (no++) * 2
ans = 2 + 2 * 2 // no = 3 after this
ans = 2 + 4
ans = 6
----------------------------------------------------
The rest is left to the student ...
Regards,
Manfred.
 
Rajeshwari, Ramamurthi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for your help
so precedency is same for both post-fix and pre-fix operators and associativity is from left to right for both. Am I right?
Raji

Originally posted by Manfred Leonhardt:
Hi,
I will try and walk you through your examples:
no = 1;
ans = ++no + 2*++no;
Compiler does:
ans = (++no) + 2 * (++no)
ans = (1+1) + 2 * (++no) // no = 2 after this
ans = 2 + 2 * (++no)
ans = 2 + 2 * (2+1) // no = 3 after this
ans = 2 + 2 * 3
ans = 2 + 6
ans = 8


 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, evaluation is always left to right as defined in the Java Language specification here
Postfix and prefix operators have the same precedence.
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry to beat on the subject: evaluation is always from left to rigth? no excceptions?
 
I think he's gonna try to grab my monkey. Do we have a monkey outfit for this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic