3.5-Relational, Comparisson & Logical Operators Flashcards
📝️ What are the Comparisson operators?
== | != -> Operators that can be used to check equality
-> Are the same? | Are different values?
🤯️⚠️📣️ There’s a semantic difference between:
-> “Two objects are the same” and…
-> “Two objects are equivalent.”
👀️🧠️ However for numeric and boolean primitives, there is no such distinction.
What are the four scenarios for comparisson operators? and what are its PITFALL cases?
[SCENARIOS 1° boolean type operands 2° numeric primitive types operands 3° char primitive types operands 4° Reference types operands
[PITFALLS] 🤯️⚠️📣️ We cannot mix and match types ❌true == 3 | ❌false != "Grape" | ❌10.2 == "Koko" //DOES NOT COMPILE However... ✅ 5 == 5.0
🤯️⚠️📣️ Two references are equal only if they point to the same object or both point to null.
🤯️⚠️📣️ Comparing null with other null value return true -> (null == null) //RETURNS TRUE
What are the Relational operators and its PITFALL case(s)?
[PRECEDENCE]
>, >=, < , <= , instanceof
[PITFALLS]
🤯️⚠️📣️ If null is used on the left side of the instanceof operator it returns FALSE
❌The code won’t compile❌
🤖️ If null is used on the right side of the instanceof operator
🤖️ If left-type cannot possibly hold right-type ❌The code won’t compile❌
e.g: Integer instanceOf String
What are the logical operators and its precedence?
(Bitwise | Logical Operators)
&, |, ^, &&, ||
📝️ What are the logical operators behavior?
& AND is only true if both operands are true.
| Inclusive OR is only false if both operands are false.
^ Exclusive OR is only true if the operands are different.
What are the short-circuit Logical operators?
&&, ||
📝️ What does short-circuit means? The second operand is evaluated only if required.