Relational Operators Flashcards
Define all java relational operators
JAVA has 6 relational operators :
- != ( NOT equal)
- == (equal)
- > = ( greater than or equals )
- > (greater than)
- <= ( greater than or equals)
- < ( less than )
Define the relationship between numeric
promotion and relational operators for
primitive data types !
All primitive data types except boolean that act as operands of any binary relational operator are promoted to the wider data type of the two operands
Here’s a comprehensive list of the relational operators in Java along with the compatible data types for each:
- != (not equal)
Compatible Types :
All primitive types (int, char, double, float, long, short, byte, boolean)
Object types (compares references, not values)
- == (equal)
Compatible Types:
All primitive types
Object types (compares references, not values)
- > (greater than)
Compatible Types:
Numeric types (int, char, double, float, long, short, byte)
- < (less than)
Compatible Types:
Numeric types
- > = (greater than or equal to)
Compatible Types:
Numeric types
- <= (less than or equal to)
Compatible Types:
Numeric types
NOTE
Primitive Numeric Types: int, char, double, float, long, short, byte
Boolean: Used only with != and ==
Object Types: Used with == and != for reference comparison
Note that relational operators are not applicable to boolean types, except for equality and inequality
Give common use cases of instanceOf
java relation operator !
The instanceof operator is a unary operator because it checks if the left operand (an object reference) is an instance of the type specified by the right operand, which can be a class or an interface. It returns true if the object is of the same type or a subtype of the specified class or interface.
NOTE
The instanceof operator in Java requires the following data types:
- Object Type: The left operand must be a reference type (i.e., an object or null).
- Class or Interface Type: The right operand must be a class name or an interface type.