Relational Operators Flashcards

1
Q

Define all java relational operators

A

JAVA has 6 relational operators :

  1. != ( NOT equal)
  2. == (equal)
  3. > = ( greater than or equals )
  4. > (greater than)
  5. <= ( greater than or equals)
  6. < ( less than )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Define the relationship between numeric
promotion and relational operators for
primitive data types !

A

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:

  1. != (not equal)

Compatible Types :

All primitive types (int, char, double, float, long, short, byte, boolean)

Object types (compares references, not values)

  1. == (equal)

Compatible Types:

All primitive types
Object types (compares references, not values)

  1. > (greater than)

Compatible Types:

Numeric types (int, char, double, float, long, short, byte)

  1. < (less than)

Compatible Types:

Numeric types

  1. > = (greater than or equal to)

Compatible Types:

Numeric types

  1. <= (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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Give common use cases of instanceOf
java relation operator !

A

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:

  1. Object Type: The left operand must be a reference type (i.e., an object or null).
  2. Class or Interface Type: The right operand must be a class name or an interface type.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly