jQuery in Action, 3rd edition
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

Khalid M Operator and Assigment question

Siva Sivaraman

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;
Why is the answer ((++k)+ (k++))+(+k)->((2)+(2))+(3))-->7
When I tried using Maha Anna's technique
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;
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

Joined: Sep 26, 2002
Posts: 17
Thanks for the reply.
Here is the link for Maha Anna's techique...
Siva Sivaraman

Joined: Sep 26, 2002
Posts: 17
I got it right this time.
The correct way of applying the technique is:
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.
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...
I agree. Here's the link: http://aspose.com/file-tools
subject: Khalid M Operator and Assigment question
It's not a secret anymore!