Java Programming : Character
Dr. May Phyo Thu
Lecturer
https://blogs.ashrithgn.com/
Objectives
• To understand Character class and use them to develop programs.
• To know the Character class contains the methods isDigit, isLetter,
isLetterOrDigit, isLowerCase, isUpperCase, toLowerCase and toUpperCase
2 Faculty of Computer Science https://blogs.ashrithgn.com/
Character Class
• In Java, single characters are represented using the data type char.
• Character constants are written as symbols enclosed in single quotes.
• Characters are stored in a computer memory using some form of
encoding.
• ASCII, which stands for American Standard Code for Information
Interchange, is one of the document coding schemes widely used today.
• Java uses Unicode Worldwide Character Standard (Unicode) , which
includes ASCII, for representing char constants.
3 Faculty of Computer Science https://blogs.ashrithgn.com/
ASCII Code for Commonly Used Characters
Chara Code Value in Unicode
cters Decimal Value char letter = 'A'; (ASCII)
char numChar = '4'; (ASCII)
‘0’ to \u0030 to \
48 to 57
‘9’ u0039
‘A’ to \u0041 to \
65 to 90
‘Z’ u005A
char letter = '\u0041'; (Unicode)
‘a’ to \u0061 to \ char numChar = '\u0034'; (Unicode)
97 to 122
‘z’ u007A
4 Faculty of Computer Science https://blogs.ashrithgn.com/
Methods in the Character Class
Method Description
isDigit(ch) Returns ture if the specified character is a digit.
isLetter(ch) Returns ture if the specified character is a letter.
isLetterOrDigit(ch) Returns ture if the specified character is a letter or digit.
isLowerCase(ch) Returns ture if the specified character is a lowercase
letter.
isUpperCase(ch) Returns ture if the specified character is a uppercase
letter.
toLowCase(ch) Returns the lowercase of the specified character.
toUpperCase(ch) Returns the uppercase of the specified character.
5 Faculty of Computer Science https://blogs.ashrithgn.com/
isLetter(char) Method
public class CharacterTest {
public static void main(String[] args)
{
char ch = 'A';
boolean b1 = Character.isLetter(ch);
System.out.println(ch + " is letter: " + b1);
}
}
A is letter: true
6 Faculty of Computer Science https://blogs.ashrithgn.com/
isDigit(char) Method
public class CharacterTest {
public static void main(String[] args)
{
char ch = ‘A';
boolean b1 = Character.isDigit(ch);
System.out.println(ch + " is digit: " + b1);
}
}
A is digit: false
7 Faculty of Computer Science https://blogs.ashrithgn.com/
isLetterOrDigit(char)Method
public class CharacterTest {
public static void main(String[] args)
{
char ch = 'A';
boolean b1 = Character.isLetterOrDigit(ch);
System.out.println(ch + " is letter or digit: " + b1);
}
}
A is letter or digit: true
8 Faculty of Computer Science https://blogs.ashrithgn.com/
isLowerCase(char) Method
public class CharacterTest {
public static void main(String[] args){
char ch = 'A';
boolean b1 = Character.isLowerCase(ch);
System.out.println(ch + "is lowercase letter: " + b1);
}
}
A is lowercase letter: false
9 Faculty of Computer Science https://blogs.ashrithgn.com/
isUpperCase(char)Method
public class CharacterTest {
public static void main(String[] args){
char ch = 'A';
boolean b1 = Character.isUpperCase(ch);
System.out.println(ch + " is uppercase letter: " + b1);
}
}
A is uppercase letter: true
10 Faculty of Computer Science https://blogs.ashrithgn.com/
isWhitespace(char) method
public class CharacterTest {
public static void main(String[] args){
char ch = ‘A ';
boolean b1 = Character.isWhitespace(ch);
System.out.println("' ' is a white space character: " + b1);
}
}
A is a white space character: false
11 Faculty of Computer Science https://blogs.ashrithgn.com/
toUpperCase(char) Method
public class CharacterTest {
public static void main(String[] args){
char ch = ‘A';
char ch3 = Character.toUpperCase(ch);
System.out.println(ch + " is converted to " + ch3);
}
}
A is converted to A
12 Faculty of Computer Science https://blogs.ashrithgn.com/
toLowerCase(char) Method
public class CharacterTest {
public static void main(String[] args){
char ch = 'A';
char ch3 = Character.toLowerCase(ch);
System.out.println(ch + " is converted to " + ch3);
}
}
A is converted to a
13 Faculty of Computer Science https://blogs.ashrithgn.com/
Reading a Character from the Console
1 public class StringTest {
2 public static void main(String[] args) {
3 Scanner input = new Scanner(System.in);
4 System.out.print("Enter a character: ");
5
String s1 = input.next();
6 char ch = s1.charAt(0);
7 System.out.println("The character entered is " + ch);
8 }
9}
14 Faculty of Computer Science https://blogs.ashrithgn.com/
Summary
• The Character class contains the methods isDigit, isLetter, isLetterOrDigit,
• isLowerCase, isUpperCase for testing whether a character is a digit, letter,
lowercase, and uppercase.
• It also contains the toLowerCase and toUpperCase methods for returning a
lowercase or uppercase letter.
Next Topic : String
15 Faculty of Computer Science https://blogs.ashrithgn.com/