Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

andren
andren
28,558 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