File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Majji Mock Exam # 1

 
Rajiv Ranjan
Ranch Hand
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: }

If run the output is True and the reason as per author is
"Conditional operators have high precedence than assignment operators"
Can someone explain this to me in more details.
Thanks,
Rajiv
 
Brian, Cavanagh
Ranch Hand
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rajiv,
What happens is that when the compiler sees the statement
(b=i==j), it will interpret it as (b=(i==j). The conditional operator will be executed first, and it returns true, so true is assigned to boolean b, and therefore b = true, and this is outputted.
Hope this helps
Brian
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic