Java Java Basics Perfecting the Prototype Censoring Words - Using Logical ORs

Can we use statement like this to combine words?

if(firstName.equalsIgnoreCase(thirdExample || secondExample))
     console.printf("first is equal to second and third")

1 Answer

28,463 Points

No you cannot. The OR operator is used exclusively with boolean expressions or values.

When you use an OR Java will first look at the value on the left and see if it evaluates to true or false, then it will look at the value on the right and look if it it evaluates to true or false. If both evaluates to false it will return false, if either of them evaluate to true it will return true.

And that is they only way the OR operator can be used, with boolean values or things that evaluate to boolean values, you cannot use it to provide multiple values to a method like you are attempting.

The code you provide as an example would crash because a String is not a boolean value or expression that evalutes to a boolean value. The same principle holds true for the && (AND) operator as well.

Thank you