• 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

Dan's Operator C Exam

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..??
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Barkat.. i think it was really helpful!
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Please do not shoot the fish in this barrel. But you can shoot at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic