Introduction to Java

Equality Operators

You may have noticed that the relational operators did not include an operator for testing "equals to". In Java, equality operators are used to test equality.

The equality operators are:

  1. ==: equal to.
  2. !=: not equal to.

Equality operators do not require that operands share the same ordering. For example, you can test equality across boolean, char, or int data types. The example below combines assigning variables and using an equality operator:

char myChar = 'A';
int myInt = -2;
System.out.println(myChar == myInt);

The example above will print out false because the value of myChar ('A') is not the same value as myInt ('-2').

Community Forums
Get help and ask questions in the Codecademy Forums
Report a Bug
If you see a bug or any other issue with this page, please report it here.