Compare two char values

In this chapter you will learn:

  1. How to compare two characters

Compare two characters

  • int compareTo(Character anotherCharacter) compares two Character objects numerically.
  • boolean equals(Object obj) compares this object against the specified object.
public class Main {
  public static void main(String[] argv) {
    Character character1 = new Character('a');
    Character character2 = new Character('b');
  }/*  ja v a 2  s.  co  m*/

The output:

The following code makes the output more meaning full by printing out different message for the result from compareTo method.

public class Main {
  public static void main(String args[]) {
    Character a = new Character('a');
    Character a2 = new Character('a');
    Character b = new Character('b');
/*j a  v a 2s.c o  m*/
    int difference = a.compareTo(b);

    if (difference == 0) {
      System.out.println("a is equal to b.");
    } else if (difference < 0) {
      System.out.println("a is less than b.");
    } else if (difference > 0) {
      System.out.println("a is greater than b.");

    System.out.println("a is " + ((a.equals(a2)) ? "equal" : "not equal")
        + " to a2.");


The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Is Character a upper/lower Case
  2. How to Change character case
Home » Java Tutorial » Primitive Data Types


    Java Primitive Data Types


    Java boolean type
    Java boolean type conversion
    Convert string value to boolean
    Convert boolean to string


    Java char type
    Compare two char values
    Change char case
    Java char conversion
    Java char value and its attributes


    Java byte type
    Convert Byte to String
    Convert String to byte
    Byte object constructor
    Byte's max and min value
    Compare two byte values
    Convert Byte to byte, double, float, int, long and short


    Java short type
    Short min/max value and size
    Create Short object
    Compare short values
    Convert short to String
    Convert Short to primitive types
    Convert string to short
    Reverse bytes


    Java int type
    int max/min value
    Create Java integer
    Convert int to binary, hexadecimal and octal format
    Compare integer values
    Integer sign
    Convert string to int
    Convert int to primitive types
    Convert int to String
    int bit operations


    Java long type
    Compare two long values
    Convert long to binary, hex and octal
    Convert long value to primitive types
    Convert String to long value
    Convert long to String


    Java float type
    Java float type conversion
    Predefined value for float type
    Compare two float value


    Java double type
    Deal with NaN double value
    Compare two double values
    Java double type creation and comparison
    Java double type conversion

Data Type Conversion

    Java Automatic Type Conversion and Casting
    Data type casting
    Java type promotion
    Autoboxing and auto-unboxing