"Equality" Operators : Comparison Operators « Operators « SCJP






== equals (also known as "equal to")

!= not equals (also known as "not equal to")

class MainClass {
  public static void main(String[] args) {
    System.out.println("char 'a' == 'a'? " + ('a' == 'a'));
    System.out.println("char 'a' == 'b'? " + ('a' == 'b'));
    System.out.println("5 != 6? " + (5 != 6));
    System.out.println("5.0 == 5L? " + (5.0 == 5L));
    System.out.println("true == false? " + (true == false));
  }
}
char 'a' == 'a'? true
char 'a' == 'b'? false
5 != 6? true
5.0 == 5L? true
true == false? false








2.6.Comparison Operators
2.6.1.Comparison Operator Summary
2.6.2.Comparison operators <, <=, >, >=, = =, and != return a boolean result.
2.6.3.Comparison operators are commonly used to form conditions
2.6.4.The Ordinal Comparisons Operators: <, <=, >, and >=
2.6.5.It is acceptable to compare the float value to the char variable c.
2.6.6.Ordinal comparisons are not applicable to any non-numeric types.
2.6.7.The Equality Comparison Operators: == and != for primitive types
2.6.8.The Equality Comparison Operators: == and != for variables of object type
2.6.9.You should not use these operators to compare the contents of objects.
2.6.10.To do a content comparison, use equals() method rather than the == or != operator.
2.6.11.For object references, the == operator returns true only if both references are to the same object.
2.6.12.Define your own equals Method
2.6.13.The == with Strings Trap
2.6.14."Equality" Operators
2.6.15.Equality for Reference Variables
2.6.16.Equality for Enums