• 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

unary operators!

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
code:
public class Myclass {
public static void main(String args[]) {
int k = 1;
int i = ++k + k++ + +k;
System.out.println(i);
}
}
what would be the value of i here? if u can please explain the expression(++k + k++ + +k), it would be helpful a lot.(AFAIK, the postfix unary ops have higher precedence then the prefix unary operators. So i think first of all k++ will be evaluated. Am i right?)
thanx.
ashok.
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
7
++k + k++ + +k
=
(2) + (2) + (+3);
Operator precedence doesn't actually matter here, as '++' cannot be replaced with '+ +' and still be an increment operator.
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But should we expect such kind question in real test?
Guoqiao

Originally posted by David Garland:
7
++k + k++ + +k
=
(2) + (2) + (+3);
Operator precedence doesn't actually matter here, as '++' cannot be replaced with '+ +' and still be an increment operator.


 
reply
    Bookmark Topic Watch Topic
  • New Topic