This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring 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:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Question by Sreenivasa Kumar Majji  RSS feed

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
sing
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!