Signup/Sign In

Java Character class

The Character class is a wrapper that is used to wrap a value of the primitive type char in an object. An object of class Character contains a single field whose type is char.

This class provides a large number of static methods for determining a character's category (lowercase letter, digit, etc.) and for converting characters from uppercase to lowercase and vice versa.

This class is located into java.lang package and the declaration is like below.

Declaration

public final class Character extends Object implements Serializable, Comparable<Character>

Here we are explaining methods of Character class with the help of examples.

1. isLetter(char ch)

This method is used to determine if the specified character is a letter or not. It returns a boolean value either true or false. If the character is a letter then it returns true else false. It takes a single char type argument. General syntax of the method is given below.

Syntax:

	
boolean isLetter(char ch) 
	

Example:

Lets take an example to check whether the specified character is a letter or not. We used isLetter() method to ensure this. See the below example.

	
public class CharacterDemo1
{ 
    public static void main(String[] args) 
    { 
		System.out.println(Character.isLetter('S'));  

		System.out.println(Character.isLetter('9'));     
     } 
}
	

character-class-example

2. isDigit(char ch)

The Character class provides a isDigit() method to determine if the specified character is a digit or not. It returns true if the specified character is a digit, false otherwise.

It takes a single char type argument. General syntax of the method is given below.

Syntax:

	
boolean isDigit(char ch)
	

Example:

In this example, we are checking of the specified character is a digit or not. It returns true if the specified character is a digit.

	
public class CharacterDemo1
{ 
    public static void main(String[] args) 
    { 
		System.out.println(Character.isDigit('S'));  

		System.out.println(Character.isDigit('9'));     
     } 
}
	

character-class-example

3. isWhitespace(char ch)

This method is used to determine whether the specified character is a white space or not. A character is a Java whitespace character if it is a Unicode space character. It returns true if the character is a Java whitespace, false otherwise.

It takes a single char type argument. General syntax of the method is given below.

Syntax:

	
boolean isWhitespace(char ch)
	

Example:

We can use isWhitespace() method to check whether the specified character is a whitespace or not, as we did in the below example.

	
public class CharacterDemo1
{ 
    public static void main(String[] args) 
    { 
		System.out.println(Character.isWhitespace('S'));  
		System.out.println(Character.isWhitespace(' '));  
		System.out.println(Character.isWhitespace('\n'));  
		System.out.println(Character.isWhitespace('\t'));   
		System.out.println(Character.isWhitespace(5)); 
		System.out.println(Character.isWhitespace('5'));     
     } 
}
	

character-class-example

4. isUpperCase(char ch)

The isUpperCase() method is used to determine if the specified character is an uppercase character or not. It returns true if the specified character is upper case letter, false otherwise.

It takes a single char type argument. General syntax of the method is given below.

Syntax:

	
booleanisUpperCase(char ch)
	

Example:

Lets take an example to check whether the specified character is uppercase letter or not.

	
public class CharacterDemo1
{ 
    public static void main(String[] args) 
    { 
		System.out.println(Character.isUpperCase('S')); 
		System.out.println(Character.isUpperCase('s')); 
		System.out.println(Character.isUpperCase(89));      
     } 
}
	

character-class-example

5. isLowerCase(char ch)

This method is used to check whether the specified character is lowercase letter or not. It returns true if the specified character is in lowercase, false otherwise. Syntax of the method is given below.

Syntax:

	
boolean isLowerCase(char ch)
	

Example:

To check the character lower case, we can use isLowerCase() method that returns true.

	
public class CharacterDemo1
{ 
    public static void main(String[] args) 
    { 
		System.out.println(Character.isLowerCase('S'));  
		System.out.println(Character.isLowerCase('s'));  
		System.out.println(Character.isLowerCase(46));    
     } 
}
	

character-class-example

6. toUpperCase(char ch)

This method is used to convert the character argument to uppercase. It returns a character after converting to the uppercase. It takes a single argument of char type. Syntax of the method is given below.

Syntax:

	
char toUpperCase(char ch)
	

Example:

In case if we want to convert a character into uppercase then we can use this method that returns a character after converting to the uppercase.

	
public class CharacterDemo1
{ 
    public static void main(String[] args) 
    { 
		System.out.println(Character.toUpperCase('s'));  
		System.out.println(Character.toUpperCase(45));  
		System.out.println(Character.toUpperCase(12)); 
     } 
}
	

character-class-example

7. toLowerCase(char ch)

This method is used to convert the character argument to lowercase letter. It returns a character after converting to the lowercase. It takes an single character type argument. Declaration of this method is given below.

Syntax:

	
char toLowerCase(char ch)
	

Example:

we are creating an example that convert a character to its lowercase. It uses toLowerCase() method for character case conversion.

	
public class CharacterDemo1
{ 
    public static void main(String[] args) 
    { 
		System.out.println(Character.toLowerCase('S'));  
		System.out.println(Character.toLowerCase(45));  
		System.out.println(Character.toLowerCase(12));
     } 
}
	

character-class-example

8. toString(char ch)

The toString() method is used to get a String object representing the specified char. The result is a string of length 1 consisting solely of the specified char.

This method takes a single character as an argument and returns a string. Declaration of this method is given below.

Syntax:

	
String toString(char ch)
	

Example:

	
public class CharacterDemo1
{ 
    public static void main(String[] args) 
    { 
		System.out.println(Character.toString('S'));  
		System.out.println(Character.toString('a'));  
     } 
}
	

character-class-example

9. charCount(int codePoint)

This method is used to determine the number of char values needed to represent the specified character

It takes an int type argument as code point and returns 2, if the specified character is equal to or greater than 0x10000, 1 otherwise.

Syntax:

	
public static intcharCount(int code)
	

Example:

Lets take an example to use charCount() method that will count char for the code point.

	
public class CharacterDemo1
{ 
    public static void main(String[] args) 
    { 
		int a = 0x9001; 
		int b = Character.charCount(a); 
		System.out.println(b);   
     } 
}
	

character-class-example

10. charValue()

The charValue() method is used to get a primitive char type value from the char object. It returns a char value of this Character object. Declaration of this method is given below.

Syntax:

	
public char charValue()
	

Example:

In this example, we are getting a primitive char from the Character object by using the charValue() method.

	
public class CharacterDemo1
{ 
    public static void main(String[] args) 
    { 
		Character a = new Character('a'); 
		char b = a.charValue(); 
		System.out.println(b);   
     } 
}
	

character-class-example