Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
We will be looking into the Character Class of Java. It is also called wrapper class in Java, and this name is given to it because of its work of wrapping the primitive data type (generally char type) to its object. Consider this article to learn more aboutData types in Java. Character Class also provides us with a constructor to create an object.
In this article, we will get ourselves acquainted with several static methods of the Character class of Java. If you are not familiar with the static keyword, check out our blog on Static Keywords in Java first.
Constructor and Field
To use any of the features of this class. First, you need to import its package java.lang package. In Java's Character class, there is only one field of type char present in the class, and also, there is only one constructor to initialize that field. How to use the constructor to initialize the field is shown below.
Character cha = Character(‘C’);
Here, character ‘C’ is assigned to the object ‘cha’.
Now, we will discuss some of the methods from the above table with examples.
1. boolean isUpperCase(char ch):
Syntax:
boolean isUpperCase(char ch)
Code:
Java
Java
public class Demo { //In this function we will check whether the character is uppercase or not public static void main(String[] args) { System.out.println("Is C is upper case: "+Character.isUpperCase('C')); System.out.println("Is n is upper case: "+Character.isUpperCase('n')); System.out.println("Is 71(ASCII value) is upper case: "+Character.isUpperCase(71)); } }
You can also try this code with Online Java Compiler
Is C is upper case: true
Is n is upper case: false
Is 71(ASCII value) is upper case: true
2. boolean isLowerCase(char ch)
Syntax:
boolean isLowercase(char ch)
Code:
Java
Java
public class Demo { //In this function, we will check whether the character is lowercase or not public static void main(String[] args) { System.out.println("Is C is lower case: "+Character.isLowerCase('C')); System.out.println("Is n is lower case: "+Character.isLowerCase('n')); System.out.println("Is 78(ASCII value) is lower case: "+Character.isLowerCase(78)); } }
You can also try this code with Online Java Compiler
public class Demo { public static void main(String[] args) { //This function will cast the character to the uppercase character System.out.println("Upper case of c is: "+Character.toUpperCase('c')); //Using ASCII value of alphabet. System.out.println("Upper case of 101 is: "+Character.toUpperCase(101)); System.out.println("Upper case of 50 is: "+Character.toUpperCase(50)); } }
You can also try this code with Online Java Compiler
Upper case of c is: C
Upper case of 101 is: 69
Upper case of 50 is: 50
4. char toLowerCase(char ch)
Syntax:
char toLowerCase(char ch)
Code:
Java
Java
public class Demo { public static void main(String[] args) { //This function will cast the character to the lowercase character System.out.println("Lower case of n: "+Character.toLowerCase('N')); System.out.println("Lower case of 101: "+Character.toLowerCase(101)); System.out.println("Lower case of 69: "+Character.toLowerCase(69)); } }
You can also try this code with Online Java Compiler
Lower case of n: n
Lower case of 101: 101
Lower case of 69: 101
5. toString(char ch)
Syntax:
String toString(char ch)
Code:
Java
Java
public class Demo { public static void main(String[] args) { //This function will change the char to string System.out.println("Changing char C to string C: "+Character.toString('C')); System.out.println("Changing char C to string C: "+Character.toString('N')); } }
You can also try this code with Online Java Compiler
Changing char C to string C: C
Changing char C to string C: N
6. boolean isWhitespace(char ch)
Syntax:
boolean isWhitespace(char ch)
Code:
Java
Java
public class Demo { public static void main(String[] args) { //This function will tell whether there is a whitespace or not. System.out.println("Back slash t have whitespace: "+Character.isWhitespace('\t')); System.out.println("9 have whitespace: "+Character.isWhitespace('9')); //ASCII value of Tab is 9. System.out.println("' ' have whitespace: "+Character.isWhitespace(' ')); System.out.println("Back slash n have whitespace: "+Character.isWhitespace('\n')); System.out.println("C have whitespace: "+Character.isWhitespace('C')); } }
You can also try this code with Online Java Compiler
Backslash t have whitespace: true
9 have whitespace: false
' ' have whitespace: true
Backslash n have whitespace: true
C have whitespace: false
7. hashCode(char ch)
Syntax:
int hashCode(char ch)
Code:
Java
Java
public class Demo { public static void main(String[] args) { //It will return the hash code of the character System.out.println("Hash code of back slash t: "+Character.hashCode('\t')); System.out.println("Hash code of C: "+Character.hashCode('C')); } }
You can also try this code with Online Java Compiler
The Character class wraps a value of the primitive type char in an object. Here's an example:
// Creating a Character object
Character charObj = Character.valueOf('A');
// Printing the Character object
System.out.println("Character Object: " + charObj);
// Converting back to primitive char
char primitiveChar = charObj.charValue();
System.out.println("Primitive char: " + primitiveChar);
Explanation:
valueOf(char c): This static method of the Character class creates and returns a Character object for the given char.
charValue(): This method retrieves the char value from the Character object.
2. Autoboxing
Java automatically converts a char into a Character object when needed, thanks to autoboxing.
// Autoboxing a char to Character
Character charObj = 'B';
System.out.println("Autoboxed Character Object: " + charObj);
Explanation:
Autoboxing simplifies code by implicitly wrapping char into a Character
3. Storing in a Collection
You can add Character objects to a collection, such as a list:
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
// List to store Character objects
List<Character> charList = new ArrayList<>();
// Adding Character objects
charList.add('C');
charList.add('D');
// Printing the list
System.out.println("Character List: " + charList);
}
}
Explanation:
You cannot store primitive char values directly in collections because collections store objects.
Java automatically converts char to Character when adding to a collection (autoboxing).
4. Using Character Utility Methods
The Character class also provides utility methods for character operations:
Character charObj = 'E';
// Checking if it is a letter
System.out.println("Is Letter: " + Character.isLetter(charObj));
// Converting to uppercase
System.out.println("Uppercase: " + Character.toUpperCase(charObj));
Explanation:
isLetter(): Checks if the character is a letter.
toUpperCase(): Converts the character to uppercase.
Frequently Asked Questions
What is a Java Character Class?
The Character class in Java is a wrapper class for the char primitive, providing utility methods for character operations like case conversion and validation.
What is an Example of a Character Class?
An example is the Character class method Character.isLetter('A'), which checks if a character is a letter, returning true in this case.
Which Package Has the Character Class in Java?
The Character class is part of the java.lang package, which is automatically imported in all Java programs.
Conclusion
We have thoroughly discussed the Character class in Java and its implementation. We discussed how its constructor, field, and methodscould be used in Java.
We hope that this blog has helped you enhance your knowledge regarding the Character class in Java. We can use the concepts of Java in building an android app to learn in-depth about android development. Check out our Android Development course on the Code360 website.
Live masterclass
Become a YouTube Analyst: Use Python to analyze viewers data
by Coding Ninjas
04 Feb, 2025
02:30 PM
Get hired as an Amazon SDE : Resume building tips
by Coding Ninjas
03 Feb, 2025
02:30 PM
Expert tips: Ace Leadership roles in Fortune 500 companies
by Coding Ninjas
03 Feb, 2025
12:30 PM
Become a YouTube Analyst: Use Python to analyze viewers data