permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Khalid M Operator and Assigment question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Khalid M Operator and Assigment question" Watch "Khalid M Operator and Assigment question" New topic
Author

Khalid M Operator and Assigment question

Siva Sivaraman
Greenhorn

Joined: Sep 26, 2002
Posts: 17
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

Joined: Aug 05, 2002
Posts: 787
I am getting 7.
akmal jah
Ranch Hand

Joined: Feb 18, 2002
Posts: 31
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

Joined: Sep 06, 2002
Posts: 43
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.


_ _____ _ <br />Used to be a Java Programmer but now I work on Microsoft Technologies - Word, Excel and Outlook!
Siva Sivaraman
Greenhorn

Joined: Sep 26, 2002
Posts: 17
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

Joined: Sep 26, 2002
Posts: 17
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

Joined: Nov 14, 2001
Posts: 57
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

Joined: Sep 06, 2002
Posts: 43
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
 
wood burning stoves
 
subject: Khalid M Operator and Assigment question
 
Similar Threads
confusing for Loop.
Operator Precendence
Garbage collection question
Recursion of Fibonacci Numbers
Operators - Mughal p.56 Ques 3.7