Help with reading Boolean operators (precedence)


I'm asked for a statement that prints out false. The one below does, however I'm a little confused as to why.

I read it like this. Brackets first: (5 >2 && 3< 5) true and true = true. Second !(1<8 = false. So with the final boolean, false OR true = true. But the statement prints false.

Any help would be great

boolean riddle = !( 1 < 8 || (5 > 2 && 3 < 5));


You got: not ( true or (true and true) ) = not( true or true ) = not true = false


thanks for the quick reply. Your answer has helped me to read the booleans a little different. Namely ! as 'not' instead of a flat 'true' of 'false'.

Do you have any pointers or online guides i could check out to get up to speed with these kind of expressions ?


Tips for using them would probably be to never trust that you remember the precedence correctly. Be liberal with your parenthesis, it's less error prone and will be easier for others to read. As for online resources or guides, I'd simply google "Java logical operators". There aren't really that many of them and they pretty much all work as you'd expect.