In Mughal's code: public class Prog1 { public static void main (Streing args [] { int k = 1; int i = ++k + k++ + + k; System.out.println(i); } } The answer: ((2) + (2) + (3)) --> 7 How does one arrive at 3? [ March 11, 2003: Message edited by: Linda Pan ]

Originally posted by Linda Pan: The answer: ((2) + (2) + (3)) --> 7 How does one arrive at 3? [ March 11, 2003: Message edited by: Linda Pan ]

Are you saying that Mughal's book says the answer should be 3? I don't have my book with me, so I can't look it up, but compile and execute that code - it outputs 7. If Mughal's book is saying that the output should be 3, I'd check the book again and make sure that you haven't made a typo. Corey

Just check the print statement System.out.println(i); Is is i/k? If it is k the value is 3. In some mocks I just picked the wrong results when picking the output after computing every thing correctly.

I think maybe the real question is not that the answer is 3 but how does k equal 3 in the expression int i = ++k + k++ + + k; if the answer is ((2) + (2) + (3)). I believe it is because by the time you get to the third k the postfix operator has executed and k equals 3. Is this correct?

Linda Pan
Ranch Hand

Joined: Sep 24, 2001
Posts: 96

posted

0

yes, John you are correct. I was wondering why '+ + k' would be 3. I guess you answered by question ... that '+ + k' is the same as a postfix therefore (k=2) 2 + 1 = 3. Somehow intuitively this doesn't make sense.

Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271

posted

0

Originally posted by Linda Pan: that '+ + k' is the same as a postfix

That's not right! k++ and + + k are two very different things. In order to be a perfix or postfix operator, the + or - operands must be directly beside each other. Having a space between them changes their meaning entirely. Check out this code:

Notice that z and y have the same resulting value, 10, but a and b don't end up being the same value. a becomes 6 while b remains at 5 - obviously, the ++ and the + + are doing different things entirely. So what is the + + operator? There is no such thing. This statement is simply two unary operators next to one another. I hope that helps, Corey

Sarma Lolla
Ranch Hand

Joined: Oct 21, 2002
Posts: 203

posted

0

So what is the + + operator? There is no such thing. This statement is simply two unary operators next to one another.

Please note that only the second one is unary operator. The first one is binary opetator. The expression is evaluated like x+ + y as x+ (+y) and then as x+y. So there is only one unari op.

Originally posted by Linda Pan: that '+ + k' is the same as a postfix

was equal to a postfix operator, I was saying that after the k++ is evaluated in the expression and execution moves on to the third argument in the expression that k is incremented by the postfix at the second position. As execution reaches the third k, k is now 3, which gives the values as ((2)+(2)+(3)).

This is in Answer to Marlene Miller How does one arrive at 3? int k = 1; int i = ++k + k++ + + k; It is calculated as follows : (Substituting the value in the expression below) int k=1; (Initial value of k is 1) int i = -> ++k = (2)(increments first, now value of k is 2) -> k++ = (2)(first prints then increments, now value of k is 3) -> +k = (3)(shows last incremented value, don't get confused +k, it just indicates that its a positive value) So if you add all the three expressions i.e. 2 + 2 + 3 you will get 7. The value 3 Which u r referrering to is the value of variable k in the third expression and not the total value of all the expressions. I hope this will solve your doubt. [ March 12, 2003: Message edited by: Manish Sachdev ] [ March 12, 2003: Message edited by: Manish Sachdev ]

original Post In Mughal's code: public class Prog1 { public static void main (Streing args [] { int k = 1; int i = ++k + k++ + + k; System.out.println(i); } } The answer: ((2) + (2) + (3)) --> 7 well the program is evaluated as 3+1+3 = 7 How lets c? post fix operator has hightest precedence then prefix operators ++k and +k evaluated { when two have same precedence then associativity works from left to right } hence when int i = ++k + k++ + + k; evaluated at place of k++ 1 is replaced and k becomes 2 when ++k evaulated 2+1 becomes 3 and then +k replaced with 3 [ by associativity rule ] so final evaluation is 3+1+3 which is 7.

hi sharana, hence when int i = ++k + k++ + + k; evaluated at place of k++ 1 is replaced and k becomes 2 when ++k evaulated 2+1 becomes 3 and then +k replaced with 3 [ by associativity rule ] so final evaluation is 3+1+3 which is 7.

are you sure?? I think the value of 'k' will be (2) (3) & (3). I just modified the Prog1 and run, I got the above answer. My modified Prog is : public class Prog1 { public static void main (String args []) { int k = 1; int i = ++k; System.out.println("k "+k); i+= k++; System.out.println("k2 "+k); i+= + k; System.out.println(i+" "+k); } }

do let me know if i m not able to understand properly.

"Walking on water and building IT Architecture from <br />specification are easy if and only if both are frozen"

sharana sharana
Greenhorn

Joined: Mar 14, 2003
Posts: 24

posted

0

hi tousif as per your program ur answer is right , But the above program is about operator precedence and hence operators should be evaluated according to their precedence as follows the following fragmented program shows it

public class Prog1 { public static void main (String args []) { int k = 1; // K++ evaluted FIRST System.out.println("k++ "+k++); // Highest priority //++K evaluated SECOND System.out.println("++k "+ ++k); //second // the value of evaluated st Second step is replced at +K here System.out.println("+k"+k); } }

I tried the follwoing code. It seems that the expression is evaluated from left to right. public class B{ public static void main(String [] args) { int j = 1;

//1 + 3 + 3 evaluate based on precedence //2 + 2 + 2 evaluate from left to right int i = ++j + +j + j++; System.out.println(i); //output 6 } }

Hello Linda I think that In Mughal's code: public class Prog1 { public static void main (String argv []) { int k = 1; int i = ++k + k++ + + k; System.out.println(i); } } The answer: ((2) + (2) + (3)) --> 7 In my opinion,the answer : ((2) + (2) + (3)) --> 7 may be wrong, because the answer before compilation should be ((2) + (2) + (2)) --> 6 But after the compilation the whole code int i = ++k + k++ + + k; k++ become 3 Because k++ will take the effect on the compilation time after the syntax ';' So, the answer should be ((2) + (3) + (2)) --> 7 Please let mw know if I right, thank you

Hi Parsing in Java takes place from left to right. ---- public class Prog1 { public static void main (Streing args [] { int k = 1; int i = ++k + k++ + + k; System.out.println(i); } } The answer: ((2) + (2) + (3)) --> 7 How does one arrive at 3? ---- The parsing takes place like this : k=1; so ++k gets the value 2 and k becomes 2; then k++ is parsed , it gets the value 2 and k becomes 3; now +k is parsed , it gets the value 3 and k becomes 3. so now, Evaluation takes places according to precedence and associativity. The only operator here is '+', so no precedence rule applies. '+' is Left To Right associative, therefore evalutaion of the expression takes place from left to right like this : ++k + k++ , which gives 2 + 2 = 4 now 4 + +k, which gives 4 + 3 = 7 Hence the answer 7. ---------- For more clearity , consider this code : class A { public static void main(String args[]) { int a=9; a+=(a+=(a=3)); System.out.println(a); int b=9; b=b+(b=3)+(b+=3); System.out.println(b); } } The output is 21 and 18. Bye Anu

Jonas Isberg
Ranch Hand

Joined: Mar 18, 2003
Posts: 118

posted

0

Originally posted by Anu Pasricha: Parsing in Java takes place from left to right.

Hi, I'm not sure whether I'm right. This is what I think the answer is 7. This statement can split into 3 steps. "int i = ++k + k++ + + k" Step 1 (++k) ----------- k = k + 1 (k = 2 now) Step 2 (k++) ------------ k = 2 Step 3 (+ k) ------------ k = 3 (result of k++ on Step 2) k = 3 because of the ++ postfix. The addition is only done on Step 3.

Dominic Choo
Greenhorn

Joined: Jan 30, 2003
Posts: 26

posted

0

That's why the answer is 2 + 2 + 3 = 7

Jonas Isberg
Ranch Hand

Joined: Mar 18, 2003
Posts: 118

posted

0

I do agree with your reasoning Dominic.

Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867

posted

0

Thank you very much Dominic & J Isberg Your description is very helpful.