Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dan's Operator C Exam

 
Saniya Ansari
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class T {
public static void main(String args[]) {
String a = "1";
byte b = 2;
short c = 3;
char d = 4;
int e = 5;
float f = 2;
a += b *= c += d *= e += f;
System.out.print(a);
}
}

The answer is 162 .. cant' figure out why its right associative and how can one rewrite it..
And wont the float=2.0 give a compile time error coz it should be float = 2.0f.. but ignoring that still can someone help!
secondly..
class C {
static int m(int i) {
System.out.print(i + ", ");
return i;
}
public static void main(String s[]) {
int i = 1;
m(m(++i) - m(i++) + ~m(-i) * -m(~i));
}
}
The answer is 2, 2, -3, -4, 8... i can go upto -3 but how do we get -4. coz for -m(~i) we send the value ~3 which is 0.
Where am i going wrong..??
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Saniya:
This code might help you. I guess in a multiple assignment situation, it starts from right most first and then goes left. I have broken the code to make the output clear. It reassign all the original values before printing next stage:

[ October 07, 2002: Message edited by: Barkat Mardhani ]
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Saniya:
For your second question, run following code and observe the result. Note that post-script (i++) and pre-script(++i) operators actually change the value of i. However uniry operator (-i) and (~i) do not change the actual value of i. They only send modified value to the called method.

[ October 07, 2002: Message edited by: Barkat Mardhani ]
[ October 07, 2002: Message edited by: Barkat Mardhani ]
[ October 07, 2002: Message edited by: Barkat Mardhani ]
 
Saniya Ansari
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Barkat.. i think it was really helpful!
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just updated the remark associated with the answer.

The compound assignment operator is right associative so the expression is evaluated from right to left. The result is a String value that can be calculated using the following expression. "1"+(2*(3+(4*(5+2))))

Is this new remark an adequate explanation?
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But Dan for following statement:
m(m(++i) - m(i++) + ~m(-i) * -m(~i));
Why is it not executing third and fourth m() calls first as * has more priority over + or -?
 
Saniya Ansari
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Barkat..
I think its becoz parenthesis have a higher precedence than any operators.. so first the parenthesis are evaluated and then only others.
Dan i think it explains it much better! Thanks!
[ October 08, 2002: Message edited by: Saniya Ansari ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic