• 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

understanding operator precedence

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
QUESTION: From the operator precedence chart, we see that postfix operators have a higher precedence, but then in the following example, why does the compiler first evaulate ++a? If it is due to the operands being evaulated from left to right, then what is the use of giving postfix operators a higher precedence (since ++a or a++ are evaulated from left to right anyways)? When will the predence make a difference?

 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It doesn't look like it would make a difference here. I was going to give you an example of ++a++ like we can do in C (at least with de-referencing pointers) ... but the compiler doesn't like that.
 
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in your example pal,


the precedence does not really look at a++ then ++a, what happens here is this.

x =
++a //prefix so a is 2
+
a++ //postfix so a is still a which is 2
so 2+2 = 4

this always goes from left to right.

x= a++ + ++a + a++ + a++ + ++a;
1 + (postfixed now to 2) prefix now to 3 + 3 + (postfixed now to 4) prefix now to 5 + prefix to 6
x=1+3+3+5+6
x=18

hope this helps

Davy
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sudarshan,

Once Maha aana had given a very cool trick to solve such type of questions. I here take the prievlege to share the same with you

a=1
x = ++a + a++
x = (2)2 + 2(3)

You have tto put the values that the variable will get via the prefix/postfix operators in brackets. Then add the values that are outside the brackets for the result.

hence here x = 4

Rads
 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, hey, you got me confused...
So you are saying that the postfix isn't evaluated before prefix?
and table at http://java.sun.com/docs/books/tutorial/java/nutsandbolts/expressions.html is wrong?

bacause if postfix to be evaluated before, you would get:

a=1
x = ++a + a++
x = (3)3 + 1(2)=4

yes, the result is still right, but

explain this:
one:
a = 1;
System.out.println(++a + " " + a++);//prints 2 2

two:
a = 1;
System.out.println(a++ + " " + ++a);//prints 1 3

in 'one' if a++ was to be evaluated first, we should have gotten 3 1, right?
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gula,

Read this.
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gula,

your post can be answered by my post above and radhika post.

a=1
System.out.println(++a +" "+ a++);

is like saying
System.out.println(preincrement to make 2 add a string space then add a which is 2); now increment a to get 3, but I am outside the parenthesis, so I don't get seen.
answer 22

two:
a = 1;
System.out.println(a++ + " " + ++a);

System.out.println(1 then increment but add a string space, you had the new value of 2 but preincrement a to 3);
answer 13

when you add a string to an int, the int gets promoted to a string.

but watch out for
a = 2;
b = 2;
System.out.println("."+a + b + " rifle"); //.4 rifle
but
System.out.println("."+a+ ""+b+ " rifle"); //.22 rifle

I hope this helps Gula.

Davy
[ July 14, 2004: Message edited by: Davy Kelly ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic