• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Operators

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok there is this question in Mughal's book.. i'm kinda confused although it seems easy..
public static void main(String args[]){
int k= 1;
int i= ++K + k++ + + k
System. out. prinln(i);
}
The answer is 7.. i'm not sure how.. HELP!
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
re-write the line as
i = (++k) + (k++) + (+k);
evaluation left to right:
i = 2 + (k++) + (+k) (k is incremented and assigned the value 2)
i = 2 + 2 + (+k) ( k's current value used, but with a side effect of incrementing k)
i = 2 + 2 + 3
i = 7;

Serdar

Originally posted by Saniya Ansari:
ok there is this question in Mughal's book.. i'm kinda confused although it seems easy..
public static void main(String args[]){
int k= 1;
int i= ++K + k++ + + k
System. out. prinln(i);
}
The answer is 7.. i'm not sure how.. HELP!

 
Saniya Ansari
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok.. thanks i got this much explanation from the book too but just cant' figure out how does the +k will get the value 3
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The (K++) will give 2 to expression but it will increase K's value to 3. Therefore, (+K) will receive 3...
[ October 06, 2002: Message edited by: Barkat Mardhani ]
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please check Maha Anna's wonderful explanation on the prefix/postfix operators:
http://www.javaranch.com/ubb/Forum24/HTML/000775.html
[ October 06, 2002: Message edited by: Claire Yang ]
 
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
CHeck out Brian Lugo's, too:
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=014701
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic