Help coderanch get a
new server
by contributing to the fundraiser
  • 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

JQ+ Operator Question!!

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Fellas,
The question goes as such:
What will be the result of compiling and running the following code be?
public class TestClass
{
public static void main(String args[])
{
int k = 1;
int i = ++k + k++ + + k;
System.out.println(i+""K);
}
}
The answer: The program compiles and prints 7 and 3
JQ+ comments that we must look at it like ((2)+(2)+(3)) = 7
End value of k is 3
Please help me with this concept!!
Thanks in Advance
Best Regards
Edmund
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Edmund Chiang:
Hi Fellas,
The question goes as such:
What will be the result of compiling and running the following code be?
public class TestClass
{
public static void main(String args[])
{
int k = 1;
int i = ++k + k++ + k;
System.out.println(i+""K);
}
}
The answer: The program compiles and prints 7 and 3
JQ+ comments that we must look at it like ((2)+(2)+(3)) = 7
End value of k is 3
Please help me with this concept!!
Thanks in Advance
Best Regards
Edmund



Hi Edmund,
This has to do with pre-increment vs. post-increment operators. As JQ+ suggested, you should look at the expression as:
int i = ((++k) + (k++) + k);

++k (pre-increment ++operator) says "increase my value by 1 before using it in the expression." k is therefore will take a new value of 2 and the above expression will look like this:
int i = ((2) + (k++) + k);
k++ (post-increment operator++) says "use my current value before increase its value by 1." So in the above expression, (k++) will still be evaluated with a value of 2... hence the above expression looks like this:
int i = ((2) + (2) + k);
After the post-increment, k = 3 and the above expression finally looks like this:

int i = ((2) + (2) + 3) => i = 7, k = 3;
Hope that helps,
Lam
 
Edmund Chiang
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah Definitely!!
Thanks Lam that really helped me clear up my concept about that topic. I was 90% sure that that was the answer but thanks for confirming it.
Best Regards
Edmund
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was a little surprised by the ending " + + k". Is there any significance to " + + " or is it just taken as "+0+" by the parser? I notice you can also do this and get the same result:
int i = ++k + k++ + + + + + + + + + + + + + k;
Thanks,
- Dave

 
She still doesn't approve of my superhero lifestyle. Or this shameless plug:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic