37 Slides IntelliJ Basics Assignment Operator Vs Equals To Operator
37 Slides IntelliJ Basics Assignment Operator Vs Equals To Operator
Operators
As you can see, we've used the assignment operator (one equal sign) in the if
statement.
What we need to do, is to use the "equals to" operator (two equal signs).
We're not assigning a value here, instead we want to test if the values are equal to
each other.
In the code above, we are testing if the value in is car is true. As you can see on the
previous line, we assigned it to be false.
If we use the "not" operator, we are testing for the opposite value of the is car
variable. We assigned is car on the previous line to false, so not is car, would return
true.
COMPLETE JAVA MASTERCLASS
Assignment Operator VS Equals to Operator
The NOT
Operator
I'd generally recommend using the abbreviated form, if your variables are booleans,
for two reasons.
One, It's much harder to identify the error, if you accidentally use an assignment
operator.
As we saw, IntelliJ won't flag this as an error when you're testing a boolean variable,
so the only way you'll know you made this common mistake is by discovering your
program or output isn't what you expected.
Secondly, the code is more concise, and more concise code can often be more
readable code.