1.

Compare Characters in Java

Answer»

The Java provides compareTo() method to COMPARE two characters.
Create characters:

Character c1 = new Character('s'); Character c2 = new Character('p');

Now let us compare them:

public class Example {     public static VOID main(String []ARGS) {        Character c1 = new Character('s');        Character c2 = new Character('p');        if ((c1.compareTo(c2)) == 0) {          System.out.println("Equal!");        } else if ((c1.compareTo(c2)) < 0) {          System.out.println("c1 is less than c2");        } else if ((c1.compareTo(c2)) > 0) {          System.out.println("c1 is less than c2");        }     } }

The OUTPUT:

c1 is less than c2


Discussion

No Comment Found