aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Operators - Mughal p.56 Ques 3.7 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Operators - Mughal p.56 Ques 3.7" Watch "Operators - Mughal p.56 Ques 3.7" New topic
Author

Operators - Mughal p.56 Ques 3.7

Linda Pan
Ranch Hand

Joined: Sep 24, 2001
Posts: 96
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 ]
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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


SCJP Tipline, etc.
Sarma Lolla
Ranch Hand

Joined: Oct 21, 2002
Posts: 203
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.
John Hembree
hired gun
Ranch Hand

Joined: Mar 07, 2003
Posts: 250

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
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
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

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.
John Hembree
hired gun
Ranch Hand

Joined: Mar 07, 2003
Posts: 250

I wasn't saying that

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)).
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
How does one arrive at 3?
In my copy of Mughal's book (which I think is the 4th printing) the answer to problem 3.7 is
(d) The expression "++k + k++ + + k" is parsed as ((++k) + (k++)) + (+k) -> ((2) + (2) + (3)), therefore the program prints 7 when run.

[ March 11, 2003: Message edited by: Marlene Miller ]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Here is a good discussion about this:
http://www.coderanch.com/t/190825/java-programmer-SCJP/certification/Array


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
M Sharma
Ranch Hand

Joined: Dec 13, 2001
Posts: 106
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 ]

Regards, Manish
SCJP 1.4
Linda Pan
Ranch Hand

Joined: Sep 24, 2001
Posts: 96
Oh I see. Thank you all for your insight!
sharana sharana
Greenhorn

Joined: Mar 14, 2003
Posts: 24
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.



sharana
Tausif Khanooni
Ranch Hand

Joined: Nov 14, 2002
Posts: 107
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
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); }
}
Lih Chang
Greenhorn

Joined: Mar 21, 2003
Posts: 19
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
}
}
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
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


Francis Siu
SCJP, MCDBA
Jonas Isberg
Ranch Hand

Joined: Mar 18, 2003
Posts: 118
I think that my little program will explain which addition really takes place.

The result is:
((2) + (2) + (3)) --> 7
I would be most grateful if anyone could point me to someplace which explains which operator is evaluated when.
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi J Isberg
Thank you

But I have confused by the code.
When will k++ take place?
I wonder if anyone could answer the above question
thank you for your time
Anu Pasricha
Greenhorn

Joined: Jul 08, 2002
Posts: 13
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
Originally posted by Anu Pasricha:
Parsing in Java takes place from left to right.

Paragraph 1 of chapter 15.7 in Java Language Specification ( http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#4779 ):
The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right.
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
Thank you for Anu Pasricha & J Isberg answering my question and give me some reference material
Now I must not be confused.Thank you
Dominic Choo
Greenhorn

Joined: Jan 30, 2003
Posts: 26
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
That's why the answer is 2 + 2 + 3 = 7
Jonas Isberg
Ranch Hand

Joined: Mar 18, 2003
Posts: 118
I do agree with your reasoning Dominic.
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
Thank you very much Dominic & J Isberg
Your description is very helpful.
 
 
subject: Operators - Mughal p.56 Ques 3.7