How to compare characters in Java
In this tutorial, we will learn about how to compare characters in Java. To compare characters in Java, we will learn about what is a character in Java
Char
The character is a data type with a name char. This data type only stores a single character inside it. The size of char data type is two bytes.
char data type ranges from value:
Zero to Sixty-five thousand five hundred and thirty-five (0 to 65535).
Char is also a primitive data type in Java.
Char can be used as how an int, float data types are used.
Input Statements for Char
We already know that we need to create an object for Scanner class to take inputs dynamically or during run time.
The input statement used for char is:
Syntax:
char variable_name = object_name.next().charAt(0);
Examples:
Scanner sc = new Scanner(System.in); // created for taking dynamic input
char c = sc.next().charAt(0);
char character = sc.next().charAt(0);
Note:
We can also create character arrays using char data type.
char c [ ] = new char[5];
Using the above input method we insert values into the character array.
Example:
File Name: Ch.java
Rules:
- All the class names must start with capital letter.
- The class name with main method should be the name of the program.
// A Java program learning about characters.
import java.util.*;
import java.io. *;
import java.lang. *;
class Ch
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Give character input: ");
char c = sc.next().charAt(0);
System.out.println("The character is:"+c);
// A program for character array.
char ch[] = {'a','b','c','d'};
for(int i=0;i<4;i++)
{
System.out.println("The character at "+i+" is: "+ch[i]);
}
}
}
Output:
C:\new\java>java Ch
Give character input: h
The character is: h
The character at 0 is: a
The character at 1 is: b
The character at 2 is: c
The character at 3 is: d
C:\new\java>
This is how characters’ work in Java. Now let us see how characters are compared in java.
Using Relational Operators
Let us start from the basics on how to compare characters. While we started coding we always used relational operators for comparing any values. Even now as we are in beginning state, we are also going to use relational operators.
The relational operators are:
- (==) equals to
- (>) greater than
- (<) less than
- (> =) greater than or equals to
- (< =) lesser than or equals to
- (! =) not equals to
Let us explain this method using a program
Example:
File Name: Ch.java
// Character comparison.
import java.util.*;
import java.io. *;
import java.lang. *;
class Ch
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
//Two character inputs are taken into consideration.
System.out.print("Give character 1 input:");
char c1 = sc.next().charAt(0);
System.out.print("Give character 2 input:");
char c2 = sc.next().charAt(0);
System.out.println("The character 1 is:"+c1);
System.out.println("The character 2 is:"+c2);
if(c2==c1)
{
System.out.println("EQUAL");
}
else if(c2>c1)
{
System.out.println("c2 is greater");
}
else if(c2<c1)
{
System.out.println("c1 is greater");
}
}
}
Output:
Output 1:
C:\new\java>java Ch
Give character1 input: k
Give character2 input: k
The character1 is: k
The character2 is: k
EQUAL
C:\new\java>
Output 2:
C:\new\java>javac Ch.java
C:\new\java>java Ch
Give character1 input: l
Give character2 input: m
The character1 is: l
The character2 is: m
c2 is greater
C:\new\java>
Output 3
C:\new\java>javac Ch.java
C:\new\java>java Ch
Give character1 input: z
Give character2 input: a
The character1 is: z
The character2 is: a
c1 is greater
C:\new\java>
Compare Method
In Java there is an in-built method in character class known as compare method. This is used to compare the characters present in the program.
Syntax:
int variable_name = Character. compare (character 1, character 2);
Examples:
int comp = Character. compare (c1, c2);
Example:
// Character comaprison.
import java.util.*;
import java.io. *;
import java.lang. *;
class Ch
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
//Two character inputs are taken into consideration.
System.out.print("Give character1 input:");
char c1 = sc.next().charAt(0);
System.out.print("Give character2 input:");
char c2 = sc.next().charAt(0);
System.out.println("The character1 is:"+c1);
System.out.println("The character2 is:"+c2);
int comp = Character.compare(c1,c2);
if(comp==0)
{
System.out.println("EQUAL");
}
else if(comp>0)
{
System.out.println("c1 is greater");
}
else if(comp<0)
{
System.out.println("c1 is greater");
}
}
}
Output:
Output 1:
C:\new\java>java Ch
Give character1 input: k
Give character2 input: k
The character1 is: s
The character2 is: s
EQUAL
C:\new\java>
Output 2:
C:\new\java>javac Ch.java
C:\new\java>java Ch
Give character1 input: l
Give character2 input: m
The character1 is: z
The character2 is: k
c2 is greater
C:\new\java>
Output 3
C:\new\java>javac Ch.java
C:\new\java>java Ch
Give character1 input: x
Give character2 input: b
The character1 is: z
The character2 is: a
c1 is greater
Explanation:
Comp =0 then characters are equal
Comp>0 then c1 is greater than c2
Comp<0 then c1 is less than c2
This is how characters are compared in Java.