4. Operators Flashcards
What relational operators do you know ?
There are six relational operators: >, >=, <, <=, ==, and !=. The last two
(== and !=) are sometimes referred to as equality operators. Relational operators always result in a boolean value (true or false).
What happens when comparing reference variables ?
When comparing reference variables, == returns true only if both references refer to the same object.
What instanceof Operator does ?
instanceof is for reference variables only. It checks whether the object is of a particular type. The instanceof operator can be used only to test objects (or null) against class types that are in the same class hierarchy. For interfaces, an object passes the instanceof test if any of its superclasses implement the interface on the right side of the instanceof operator.
What is the difference between ++x/–x and x++/x– ?
Prefix operators (for example, ++x and –x) run before the value is used in the expression.
Postfix operators (for example, x++ and x–) run after the value is used in the expression.
int a = 0;
int b = 0;
if( ((5<7) || (++a < 10)) | b++ < 10 ) {
System.out.println(a);
System.out.println(b);
}
Console output:
0
1
|| keeps ‘a’ from being incremented, but the | allows ‘b’ to be incremented.