• 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
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

very Ugent ........10 more min on net

 
Ranch Hand
Posts: 5397
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
will plz anyone will tell me wjtz goimng on here ???
int k = 1;
int i = ++k + k++ + + k ;
TIA
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
++k + k++ + + k
just happened to be around the same Question in
Khalid A Mughal's book ...
Evaluated as ...
(++k) + (k++) + (+k)
U might find this discussion useful ..
http://www.javaranch.com/ubb/Forum24/HTML/000775.html
 
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ravish kumar:
will plz anyone will tell me wjtz goimng on here ???
int k = 1;
int i = ++k + k++ + + k ;
TIA


Ok I could be wrong here but you have to know that prefix and postfix operations are done before math operations and also the associativity that prefix unary operations are done right to left and then both postfix unary operations and normal math operations are done left to right.
So you end up with it first looking like this:
int i = (++k) + (k++) + (+k)
so then the first ++k is evaluted which is set to 2. Now since k is 2 the left associativity takes place setting k++ which means (k++) will evaluate to 2 but remember afterwards k is now 3 so in (+k) it's just doing (+3). You end up then with:
2 + 2 + 3 = 7.
Someone correct me if I'm wrong.
 
R K Singh
Ranch Hand
Posts: 5397
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks ..
now I can sleep ...
 
Rick Reumann
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jennifer Wallace:
++k + k++ + + k
just happened to be around the same Question in
Khalid A Mughal's book ...
Evaluated as ...
(++k) + (k++) + (+k)
U might find this discussion useful ..
http://www.javaranch.com/ubb/Forum24/HTML/000775.html


Wow, thanks for that link. That shortcut maha ana provided for figuring out the solutions to these kind of problems was really awesome! (Doesn't necessarily help you really figure out what is going on but it sure is great. It eliminates having to know what is going on with associativity and the pre/post operations). With the tip you will see there applied to this problem would be:
i = (++k) + (k++) + (+k)//last + doesn't matter
i = (2)2 + 2(3) + 3
now ignore all the braces info so you have:
i = 2 + 2 + 3
i = 7 !!!
Read the link provided to find out how to do that. Good stuff (IMHO)
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic