Java - Operator Equality Operator ==

What is Equality Operator"

The equality operator (==) is used in the form

operand1 == operand2 

Rules

The equality operator tests two operands for equality.

It uses the following rules:

  • Both operands must be either primitive type or reference type.
  • Mixed operands types are not allowed and results in a compile-time error.
  • A mix of numeric and boolean types is not allowed.
  • If both operands must be either numeric or boolean.
  • For primitive operands, it returns true if the both operands represent the same value; otherwise, it returns false.
  • For reference operands, it returns true if the both operands refer to the same object in memory; otherwise it returns false.

Example

Suppose there is an int variable i.

int i = 10; 

i == 10 tests whether i is equal to 10. i == 10 will evaluate to true.

Related Topics

Quiz