Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Khalid M Operator and Assigment question

 
Siva Sivaraman
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens when you try to compile and run the following program?
public class Prog1{
public static void main(String args[])
{
int k=1;
int i=++k + k++ + +k;
System.out.println(i);
}
}
Why is the answer ((++k)+ (k++))+(+k)->((2)+(2))+(3))-->7
When I tried using Maha Anna's technique
(((2)1+1(2))+(2))->4
I am not sure if I am using it right.
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting 7.
 
akmal jah
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this would be my first post for reply so bear with me for my language..
Answer for you would be that it has to do with the way prefix and postfix operators are executed.
in prefix : the current value is incremented / decremented before its assigned
in postfix : the current value is assigned first then incremented / decremented.
for ex: int i = 0;
int j = 0;
i++;
++j;
System.out.println(i); // will print 0
System.out.println(j); // will print 1
hope it helps.
 
Bishal P
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well barkat thats what the question is. Anyone will get 7 on running that piece of code and the reason is easily understood by operator precedence. So why did siva expect 4??
Siva can you please let me know what Maha Anna's technique is (the link will help). Maybe the technique is correct and you interpreted it in a wrong way.
 
Siva Sivaraman
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.
Here is the link for Maha Anna's techique...
http://www.coderanch.com/t/190825/java-programmer-SCJP/certification/Array
 
Siva Sivaraman
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it right this time.
The correct way of applying the technique is:
(((2)2+2(3))+3)-->2+2+3-->7
Note:Substitute the latest value.
 
Sharda Vajjhala
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int k = 1;
int i=++k + k++ + +k;
Read the link - very interesting. Going by that technique:
The calculation is as follows:
i = ++k + k++ + +k;
= 2(2) + 2(3) + 3
= 2 + 2 + 3
= 7
when you pre-increment, the value of k remains the samei.e, the incremented value is used in the calculation. It's only when you post-increment, that you assign or use the current value in the calculation and then increment it - hence the
2(3) in the calculation above.
That's my interpretation of the technique. Correct me if I am wrong.
Sharda
 
Bishal P
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quite a technique but seems like my old rusted brain cannot digest this..
do u know any other techniques.. i mean i know this ++ , -- from my college days so i guess i can do it but do you guys have some more techniques for other things.. do share with me...
Thanks
Bishal
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic