This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Customer Requirements for Developers and have Marcho Behler on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Majji Mock Exam # 1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Customer Requirements for Developers this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Majji Mock Exam # 1 " Watch "Majji Mock Exam # 1 " New topic

Majji Mock Exam # 1

Rajiv Ranjan
Ranch Hand

Joined: Sep 28, 2000
Posts: 61
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;
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.
Brian, Cavanagh
Ranch Hand

Joined: Oct 30, 2000
Posts: 70
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
I agree. Here's the link:
subject: Majji Mock Exam # 1
It's not a secret anymore!