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

boolean is a true or false.

! = not

|| = or

&& = and

but I still dont understand this syntax... can someone explain me what this syntax excactly does?

asked Dec 12, 2016 at 11:54
2
  • what don't you understand about it? It does exactly as you wrote. Are you asking what that specific line does? Commented Dec 12, 2016 at 11:55
  • yeah what does the specific line? Commented Dec 12, 2016 at 11:56

4 Answers 4

4

Just dissect it:

There are some comparisons such as

5 > 2 ... true
3 < 5 ... true 

Those two are pulled together using &&; so true && true --> true

1 < 8 ... true

That one is or'ed to the rest; so we get true or true --> true

Finally, not (true) --> false

Long story short: if you don't understand the whole picture, break it down into the parts you understand; and then pull those together again.

Edit: and of course, the order I am using in my explanation isn't what happens in reality. There, 1 < 8 is evaluated first; and as that is true; and "true or X" is always true, the other comparisons are not even computed.

answered Dec 12, 2016 at 11:56
Sign up to request clarification or add additional context in comments.

1 Comment

You begin with the two expressions that aren't evaluated at all.
1

The not ! operator negates what it is in front of. In this case !() it will produce the opposite of the what is inside of the parenthesis.

the || or operator checks to see if one condition or the other is true. At least one must be true for the condition to return true.

Finally the && checks both sides of the conditional statement to see if they are both true, and both of them must be true to proceed.

answered Dec 12, 2016 at 11:58

Comments

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

Let's parse it the way Java does :

  • boolean : Here comes a boolean, i.e. true or false.
  • riddle : The variable riddle is declared to be a boolean.
  • = : The boolean variable riddle is initialized with the expression on the right.
  • !(...) : It returns a boolean, the negation (=opposite) of the boolean inside of the parentheses.
    • Inside the parentheses is a bool1 || bool2 expression, where || is a "lazy OR" operator. If bool1 is true, there's no need to evaluate bool2.
    • bool1 is 1 < 8, which is true.
    • bool2 isn't evaluated
    • bool1 ||bool2 is true
  • !(...) is false
  • riddle is initialized with false

At no point in time are 5 > 2 or 3 < 5 evaluated. Eclipse warns that those 2 expressions are "dead code" and could be removed.

The whole expression could be :

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

the result would be the same and no method call would happen at all.

answered Dec 12, 2016 at 13:07

Comments

0

The problem that you have is that you initialize at the same time as you are checking if it's true or false. You cannot compare boolean with integer. If you want to do it then you need to solve it in another way by converting from one datatype to another, or involve another variable in your solution. The way you need to solve your syntax problem is by dividing it like this:

How you did it...

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

How to potentially solve it...

boolean riddle;
"...some code to decide if riddle will be true or 
false and assign it to the variable riddle..."
if (riddle == true){
"...do some code here...";
}
if (riddle == false){
"...do some code here...";
}

Or you can solve the problem by not using boolean as datatype and instead only use integers like this...

int riddle;
"...some code to decide what value riddle will 
have and assign it to the variable riddle..."
if ( riddle < 8 && riddle > 1){
 "...do some code here...";
}
answered Dec 12, 2016 at 15:28

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.