• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Operators - Mughal p.56 Ques 3.7

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
hired gun
Posts: 250
MS IE Oracle Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 250
MS IE Oracle Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)).
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a good discussion about this:
https://coderanch.com/t/190825/java-programmer-SCJP/certification/Array
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Linda Pan
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh I see. Thank you all for your insight!
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
sharana sharana
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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); }
}
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
}
}
 
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for Anu Pasricha & J Isberg answering my question and give me some reference material
Now I must not be confused.Thank you
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's why the answer is 2 + 2 + 3 = 7
 
Jonas Isberg
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do agree with your reasoning Dominic.
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much Dominic & J Isberg
Your description is very helpful.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic