Forums Register Login

Question by Sreenivasa Kumar Majji

+Pie Number of slices to send: Send
What will happen if you compile/run this code?
1: public class Q10
2: {
3: public static void main(String[] args)
4: {
5: int i = 10;
6: int j = 10;
7: boolean b = false;
8:
9: if( b = i == j)
10: System.out.println("True");
11: else
12: System.out.println("False");
13: }
14: }
A) Compilation error at line 9 .
B) Runtime error exception at line 9.
C) Prints "True".
D) Prints "False".

Answer : C
Explanation : Conditional operators have high precedence than assignment operator.
My question is : How it's works?
Thank You.
+Pie Number of slices to send: Send
The explanation says it all. Conditional operators have higher precedence over assignment. Therefore in the expression
if( b = i == j)
i==j is evaluated before b=i.
i==j is true then b is assigned true & then true is printed out.

Originally posted by SteffySY Sing:
What will happen if you compile/run this code?
1: public class Q10
2: {
3: public static void main(String[] args)
4: {
5: int i = 10;
6: int j = 10;
7: boolean b = false;
8:
9: if( b = i == j)
10: System.out.println("True");
11: else
12: System.out.println("False");
13: }
14: }
A) Compilation error at line 9 .
B) Runtime error exception at line 9.
C) Prints "True".
D) Prints "False".

Answer : C
Explanation : Conditional operators have high precedence than assignment operator.
My question is : How it's works?
Thank You.


+Pie Number of slices to send: Send
Dear Roopa,
I understand your explanation. Thank you. You have helped me 2 questions.
Steffy

[This message has been edited by SteffySY Sing (edited December 05, 2001).]
She's brilliant. She can see what can be and is not limited to what is. And she knows this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1257 times.
Similar Threads
i & j | i ^ j ^ i >> 1 == j ? i & j : i ^ j running order?
question
Question from Dans Test
operator precedence
Compilation problem with if condition
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 06:47:30.