Question:
No OR (||) operator in java?
asp4454
2010-07-04 22:20:16 UTC
I'm using an if statement and I'm trying to check if the variable is equal to multiple values at the same time. So I figured I would use the OR operator which is ||

but when I do this my IDE is telling me that || is an undefined operator for my class type???

WHAT IN THE WORLD does this mean. I've always used the OR operator so far in programming and this is leaving me dumbstruck...
Four answers:
Unca Alby
2010-07-04 22:40:11 UTC
You don't have a piece of example code nor the text of the error message, so I'm pretty much guessing here.



My guess is that you are trying to compare two things that are not both Boolean. The OR operator in Java is not the OR operator in C or C++, which will operate virtually on anything. Java is much more type strict. So when you say (a || b) then *both* expressions "a" and "b" must be a Boolean value.



If they are method calls to an object or class, the calls must return a Boolean value.
2010-07-04 22:35:38 UTC
You can use || to perform a Boolean OR on two Boolean values. Clearly, you're trying to OR with a Class instance or Class itself, which isn't allowed. You probably meant something like:



myClass.isAwesome() || yourClass.isAwesome()



but have:



myClass || yourClass



Rest assured, there is an || operator, it just only operates on Booleans (as it should).
2016-10-14 17:19:11 UTC
B task Operators There are 12 task operators: = *= /= %= += -= <<= >>= >>>= &= ^= |= all are ideal-associative ie a=b=c communities as a=(b=c) vs (a=b)=c different than the easy task operator = it is left-associative eg a+b+c = (a+b)+c all are used with primitive documents varieties different than = and += which may be used with Strings all operators of the type op = forged their effect to the type of the left-operand there is not any implicit forged with the easy task operator = in all situations, x oper= y is resembling x = x oper y as an occasion: x += y is resembling x = x + y x %= y is resembling x = x % y x |= y is resembling x = x | y
?
2016-05-04 03:45:22 UTC
You can use || to perform a Boolean OR on two Boolean values.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...