In this article, you are going to learn about Character Classes in Java Programming Language.
Just like the number classes, java provides character classes which contains several instance methods to perform various operations.
In java programming language, we generally use primitive data type ‘char’ to deal with the characters. But, sometimes programmers require character object instead of a data type for the development of java applications.
The character classes helps the programmers to deal with this situation. These classes comes along with the predefined java.lang package.
Also Read: What is Overriding in Java Programming Language?
CREATING CHARACTER OBJECT
In java programming language, you can create a character object with the help of character constructor.
Syntax:
Character obj = new Character(‘s’) ;
Example:
class W3TrainingSchool {
public static void main(String [] args) {
Character obj = new Character (‘s’) ; // Creating a character object
char c1 = obj.charValue() ;
System.out.println(c1) ;
}
}
Output:
s
Autoboxing and Unboxing
The java compiler helps to automatically wrap up the primitive data type into an object and unwraps the object back to the data type. These two processes are known asAutoboxing and Unboxing respectively, which we have already discussed in the previous article.
JAVA ESCAPE SEQUENCES
In java, Escape Sequences are used to manipulate the text inside the output statement System.out.println() according to the output we desire.
Following is a list of escape sequences used in java programming language.
\n \t
\b \f \r \’ \” \\ | Used to insert a new line to the textUsed to insert a tab to the text
Used to insert a backspace to the text Used to insert a form feed to the text Used to insert a carriage return to the text Used to insert a single quoted character to the text Used to insert a double quoted character to the text Used to insert a backslash to the text |
Example:
class EscapeSequence {
public static void main(String [] args) {
System.out.println(“Welcome to \nW3Training School\n”) ;System.out.println(“Welcome to \’W3Training School\’”) ;
System.out.println(“Welcome to \”W3Training School\””) ;
System.out.println(“Welcome to \\tW3Training School\\”) ;
}
}
Output:
Welcome to
W3Training School
Welcome to ‘W3Training School’
Welcome to “W3Training School”
Welcome to \W3Training School\
INSTANCE METHODS USED IN CHARACTER CLASSES
(1) isDigit()
This method is used to check if a given char value is a digit and will return a boolean value.
Example:
class IsDigit {
public static void main(String args[]) {
System.out.println(Character.isDigit(‘5’)) ;System.out.println(Character.isDigit(‘a’)) ;
}
}
Output:
true
false
(2) isLetter()
This method is used to check if a given char value is a letter and will return a boolean value.
Example:
class IsLetter {
public static void main(String args[]) {
System.out.println(Character.isLetter(‘5’)) ;System.out.println(Character.isLetter(‘z’)) ;
}
}
Output:
false
true
(3) isWhitespace()
This method is used to check if a given char value is whitespace, tab or a new line.
Example:
class IsWhitespace {
public static void main(String args[]) {
System.out.println(Character.isWhitespace(‘ ‘)) ;System.out.println(Character.isWhitespace(‘8’)) ;
System.out.println(Character.isWhitespace(‘\t’)) ;
System.out.println(Character.isWhitespace(‘\n’)) ;
}
}
Output:
true
false
true
true
There are some more instance method used for character classes in java programming language. They are listed below.
- isUppercase()
- isLowercase()
- toUppercase()
- toLowercase()
- toString()
Also Read : String Classes in Java Programming
We have provided you the best possible information on the use of Character Classes in Java Programming Language. Hope you like this post.For more updates and related information, stay connected to our blogs on Java Programming Language.