• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

++k + k++ + + k

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From Mughal review question 3.7:
int k = 1;
++k + k++ + + k;
I think the steps are like this (remember increment postfix has precedence over prefix):
1. k++ returns 1 to use, then increments k, so k now is 2.
++k + 1 + + k; // k is 2
2. ++k increments k and uses that value (++2) returning 3
3 + 1 + + k; //k is 3
3. now, go left to right:
so, 3 + 1 + 3 = 7
BUT, according to the book,
the expression is "parsed as
( (++k) + (k++) ) + (+k) which yields
2 + 2 + 3 = 7
I know the result is identical, but I am trying to understand operator precedence/evaluation.
Thank you for any replies.
 
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
The book is correct, first ++k is evaluated, then k++ because they are part of an normal addition which is always evaluated from left to right.
1st element: ++k
2nd element: k++
3rd element: +k
Then, the 1st is added to the 2nd element and the result of that addition is added to the 3rd element.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
test with following fragment code.
int k=1;
int k1,k2,k3,k4;
k4=(k1=++k)+(k2=k++)+(k3=+k);
result is k1=2;k2=2;k3=3!!!
 
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
jcp0.001,
Welcome to Javaranch
We'd like you to read the Javaranch Naming Policy and change your publicly displayed name (change it here) to comply with our unique rule. Thank you.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've read a nice post/article by maha anna about these kinds of problems, but I can't seem to find the link. Basically, keep track of k's value and just work your way from left to right:
- when you come across a prefix operator, add 1 to k's current value, then use this value.
- when you come across a postfix operator, use k's current value in this instance, then increment the current value by 1.
Ok, so that wasn't a very good explanation... Let's use your example:

So using the returned values, 2 + 2 + 3 = 7.
Hope this makes it clear, if not, I hope someone can find the original post/article.
 
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
I've read a nice post/article by maha anna about these kinds of problems, but I can't seem to find the link.
Here it is:
https://coderanch.com/t/190825/java-programmer-SCJP/certification/Array
 
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:
Quote from you:


The book is correct, first ++k is evaluated, then k++ because they are part of an normal addition which is always evaluated from left to right.
1st element: ++k
2nd element: k++
3rd element: +k


I understand ++k and k++ but how does +k work?
Thanks
Barkat
 
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
I understand ++k and k++ but how does +k work?
the + in +k is an unary plus which has no practical effect on k's value. +k or k yields exactly the same value. For instance, if k is 4, +k is also 4.
This is not true of the unary -, though, since if k is -4, then -k is 4 !!
For more info:JLS 15.15.3 Unary Plus Operator +
 
Paul Villangca
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, if the variable is of a type less than int (byte, short, char), the unary operator + promotes it to an int.
 
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 played around with some complex expressions, including this one, here:Marcus Green's JCHQ SCJP Discussions. Your expression is at the botton at the current time of writing this.
[ August 19, 2002: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course, anybody who actually writes code like ought to be quartered and hung.
 
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
Actually it does hurt quite a bit.
I remember the 'C-babes' used to have very popular competitions writing code like this.
[ August 19, 2002: Message edited by: Barry Gaunt ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic