Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,524 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