• 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

(++k + ++k * (k3=k++));

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can can some one explain the evaluation order of this expr.
since postfix operator has highest precedence
k++ should be evaluated first
therefore k3 should be5
but the answer is 7.
k=5;
System.out.println(++k + ++k * (k3=k++));
madhur.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please have a look at the following discussion which explains how the ++ operator works:
https://coderanch.com/t/190825/java-programmer-SCJP/certification/Array
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you bracket the expression according to precedence and grouping rules you get:
((++k) + ((++k) * ( k3 = (k++)))).
Set k=5 and scan from the left evaluating a sub-expression when you get to a closing parenthesis, like this:

So 55 is the value printed, k is 8, and k3 is 7.
[ August 29, 2002: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Barry:
++k + ++k * (k3=k++)
I thought that () have highest precedence. So the
expression (k3=k++) should be evaluated first. Right?
Thanks
Barkat
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you add a couple of println statements to print k and k3 you will get the results I gave.
Putting the parentheses around the k3=k++ does not imply that the sub-expression is evaluated first.
The (k3=k++) is taken as the second term for the multiplication, that's all.
If those parentheses were not there the expression would not compile, because the "=" has lowest precedence.
I hope a Guru will see this and confirm my analysis.
-Cheers
Barry
BTW take a look at Marcus Green's JCHQ where I analysed some other expressions in the same way.
[ August 29, 2002: Message edited by: Barry Gaunt ]
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
PLease see the following discussion:
https://coderanch.com/t/239100/java-programmer-SCJP/certification/operator-precedence
In brief, () is not an operator but a separator, so it always has the highest precedence over operator. But as Barry said, it doesn't mean that the ()-expression will be evaluated first. It is juts a means of structuring the expression in a more readable way.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BTW. When I analyse the expression ++k + ++k * (k3=k++), applying the precedence rules I treat the parentheses as "user-given", I cannot (must not) remove them.
-Barry
 
madhur jain
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well i odes not give compile error.
and j does .
that's the truth
but i don't knowhy.
madhur.
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
madhur, what i and j are you talking about?
I'm pretty sure your answer was meant to be posted in the "scope of variable" discussion...
[ August 29, 2002: Message edited by: Valentin Crettaz ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sure that is one of the symptoms of JavaRanch Junkie Syndrome (see MD). But after only 9 posts ?

[ August 30, 2002: Message edited by: Barry Gaunt ]
 
madhur jain
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah.
my mistake.sorry for that.
meant to post in scope guestion but both windows were open
madhur.
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Val:


In brief, () is not an operator but a separator, so it always has the highest precedence over operator. But as Barry said, it doesn't mean that the ()-expression will be evaluated first. It is juts a means of structuring the expression in a more readable way.


Is it not the fact that in other languages (X base for example) that () have highest precedence and are evaluated first? I love Java untill it starts to trick me with subtle difference between separater and operator...
Thanks
Barkat
[ August 30, 2002: Message edited by: Barkat Mardhani ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic