This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Dan's Operator C Exam  RSS feed

 
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..??
 
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!
 
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 ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!