Java Character Class – 8 Astonishing Methods with Syntax & Examples

Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java

Character class in Java has the function of wrapping a primitive type variable into a character object. However this should not be confused with the character datatype that we learned about in the previous articles. Java Character Class is a predefined class with a predefined set of methods like isLetter, isDigit etc.

Java character class

Java Character Class

Character Class is a wrapper class. A wrapper class in its simplest form is a class that wraps(converts) a primitive datatype to an object. Why do we need to do that? Sometimes we need to change or apply different methods to a character variable. We cannot do that unless we convert the value into an immutable object. The java.util package handles wrapper classes.

Character class in Java is immutable. Once the value of a character datatype is changed to a character object it cannot be reverted back to its form.

The basic syntax for converting a character variable to a Character object is:

Character <variable name> = new Character(<value>);

Example:

Character c = new Character(‘z’);

This converts the character c into a Character object with the value of the argument passed as ‘z’.

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

Some frequently used Character Class methods used in Java are:

Character class methods in java

1. isLetter Method in Java

This method returns a boolean value, i.e true or false. It returns true if the argument passed in the method is a letter i.e any character in between a-z and A-Z. Otherwise it returns false.

However we can also include the ascii value of the character instead of passing the character directly as an argument because Java implicitly converts all characters to its ASCII value.

Syntax:

boolean isLetter(<character>);

Example program to illustrate the use of Java isLetter method:

package com.dataflair.characterclass;
import java.util. * ;
public class IsLetter {
  public static void main(String[] args) {
    System.out.println("The value of isLetter('a') is " + Character.isLetter('a'));
    System.out.println("Value of isLetter('7') is " + Character.isLetter('7'));
    System.out.println("The value of isLetter('97') is " + Character.isLetter(97));
  }

}

Output

The value of isLetter(‘a’) is true
Value of isLetter(‘7′) is false
The value of isLetter(’97’) is true

Note: Although 97 is not a letter it is the ASCII value of ‘a’ which is a letter. Hence it evaluates to true. Whereas the number 7 neither is a letter nor the ASCII value of a letter. Hence it evaluates to false.

2. isDigit Method in Java

The isDigit method as the name suggests checks whether the parameter passed to it is a digit or not. It returns true if the argument passed is a digit. Else it returns false.

This method is particularly used in situations where we need to filter data based on its numeric or character value.

Syntax:

boolean isDigit(<character>);

An example program to illustrate the use of Java isDigit method:

package com.dataflair.characterclass;
import java.util. * ;
public class IsDigit {
  public static void main(String[] args) {
    System.out.println("The value of isDigit('b') is " + Character.isDigit('a'));
    System.out.println("Value of isDigit('7') is " + Character.isDigit('7'));
    System.out.println("The value of isDigit('8') is " + Character.isDigit('8'));
  }

}

Output

The value of isDigit(‘b’) is false
Value of isDigit(‘7’) is true
The value of isDigit(‘8’) is true

3. isWhitespace Method in Java

Sometimes when the value of a character is a space we need to know that it exists in a string. Hence we need the method of isWhitespace. It checks whether the value of the argument passed is a white space or not. However this method also returns true if the argument is a tab or new line.

Syntax:

boolean isWhitespace(<character>);

Java program to illustrate the use of Java isWhitespace method:

package com.dataflair.characterclass;
import java.util. * ;
public class IsWhitespace {
  public static void main(String[] args) {
    System.out.println("The value of isWhitespace(' ') is " + Character.isWhitespace(' '));
    System.out.println("Value of isWhitespace('\\n') is " + Character.isWhitespace('\n'));
    System.out.println("The value of isWhitespace('\\t') is " + Character.isWhitespace('\t'));
    System.out.println("The value of isWhitespace('b') is " + Character.isWhitespace('b'));
  }

}

Output

The value of isWhitespace(‘ ‘) is true
Value of isWhitespace(‘\n’) is true
The value of isWhitespace(‘\t’) is true
The value of isWhitespace(‘b’) is false

4. isUppercase Method in Java

This method is primarily used for checking whether the character passed in the argument is an UpperCase character or not. It returns true if the character is in Uppercase. If not, it returns false.

Syntax:

boolean isUpperCase(<character>);

Java Program to illustrate the use of Java isUpperCase method:

package com.dataflair.characterclass;
import java.util. * ;
public class IsUpperCase {
  public static void main(String[] args) {
    System.out.println("The value of isUpperCase('a') is " + Character.isUpperCase('a'));
    System.out.println("Value of isUpperCase('B') is " + Character.isUpperCase('B'));
    System.out.println("The value of isUpperCase('c') is " + Character.isUpperCase('c'));
    System.out.println("The value of isUpperCase('D') is " + Character.isUpperCase('D'));
  }

}

Output

The value of isUpperCase(‘a’) is false
Value of isUpperCase(‘B’) is true
The value of isUpperCase(‘c’) is false
The value of isUpperCase(‘D’) is true

5. isLowercase Method in Java

This method is primarily used for checking whether the character passed in the argument is a LowerCase character or not. It returns true if the character is in LowerCase. If not, it returns false.

Syntax:

boolean isLowerCase(<character>);

Java Program to illustrate the use of the isLowerCase method:

package com.dataflair.characterclass;
import java.util. * ;
public class IsLowerCase {
  public static void main(String[] args) {
    System.out.println("The value of isLowerCase('a') is " + Character.isLowerCase('a'));
    System.out.println("Value of isLowerCase('B') is " + Character.isLowerCase('B'));
    System.out.println("The value of isLowerCase('c') is " + Character.isLowerCase('c'));
    System.out.println("The value of isLowerCase('D') is " + Character.isLowerCase('D'));
  }

}

Output

The value of isUpperCase(‘a’) is true
Value of isUpperCase(‘B’) is false
The value of isUpperCase(‘c’) is true
The value of isUpperCase(‘D’) is false

6. toUpperCase Method in Java

This method, unlike all the methods we have studied until now, is a character returning method. This takes the argument and converts it into an uppercase alphabet. It returns an uppercase alphabet.

Syntax:

character toUpperCase(<character/ASCII value>);

Program to illustrate the use of Java toUpperCase method:

package com.dataflair.characterclass;
import java.util. * ;
public class ToUpperCase {
  public static void main(String[] args) {
    System.out.println("The value of toUpperCase('a') is " + Character.toUpperCase('a'));
    System.out.println("Value of toUpperCase('B') is " + Character.toUpperCase('B'));
    System.out.println("The value of toUpperCase('c') is " + Character.toUpperCase('c'));
    System.out.println("The value of toUpperCase(112) is " + Character.toUpperCase(112));
  }

}

Output

The value of toUpperCase(‘a’) is A
Value of toUpperCase(‘B’) is B
The value of toUpperCase(‘c’) is C
The value of toUpperCase(112) is 80

Note: When a numeric value 112 which is the ASCII value of ‘p’ is passed it returns an ASCII value of the letter ‘P’.

7. toLowerCase Method in Java

Java toLowerCase method converts the arguments passed to it into lowercase. However, if a numeric value i.e ASCII value of an UpperCase letter is passed through it returns the ASCII value of the corresponding lowercase alphabet.

Syntax:

character toLowerCase(<character/ ASCII value>);

Java program to illustrate the use of toLowerCase method:

package com.dataflair.characterclass;
import java.util. * ;
public class ToLowerCase {
  public static void main(String[] args) {
    System.out.println("The value of toLowerCase('a') is " + Character.toLowerCase('a'));
    System.out.println("Value of toLowerCase('B') is " + Character.toLowerCase('B'));
    System.out.println("The value of toLowerCase('c') is " + Character.toLowerCase('c'));
    System.out.println("The value of toLowerCase(80) is " + Character.toLowerCase(80));
  }

}

Output

The value of toLowerCase(‘a’) is a
Value of toLowerCase(‘B’) is b
The value of toLowerCase(‘c’) is c
The value of toLowerCase(80) is 112

Note: The value 80 passed is actually the ASCII value of ‘P’ which when returned as ‘p’ proves the working of the method toLowerCase.

8. toString Method in Java

This method has the responsibility of converting the character arguments passed through it, to String objects for manipulation as and when required by the programmer. ASCII values cannot be passed as arguments here.

Syntax:

String toString(<character>);

Java program to illustrate the use of the toString method:

package com.dataflair.characterclass;
import java.util. * ;
public class ToString {
  public static void main(String[] args) {
    System.out.println("The value of toString('a') is " + Character.toString('a'));
    System.out.println("Value of toString('B') is " + Character.toString('B'));
    System.out.println("The value of toString('c') is " + Character.toString('c'));
  }

}

Output

The value of toString(‘a’) is a
Value of toString(‘B’) is B
The value of toString(‘c’) is c

Few More methods are :

valueOf() Method: The valueOf() method of the Character class is a static method that returns a Character object for the specified character value. This can be useful for converting character literals or integers (ASCII values) to Character objects.

isAlphabetic Method: The isAlphabetic() method checks if the argument passed is a letter (either uppercase or lowercase). It combines the functionality of isUpperCase() and isLowerCase() methods.

Escape Sequences in Java

There are some particular characters, which when preceded with a backslash(\), holds special meaning to the compiler. Some of the escape sequences are:

  • \n- creates a new line
  • \t- creates a tab space
  • \b- inserts a single backspace
  • \r-inserts carriage return
  • \\- inserts a backslash character
  • \’- inserts single quote character
  • \” – inserts double-quote character

Summary

Finally we learnt that Java character class is one of the most important wrapper classes in Java which is used for a lot of manipulations. We also came to know about escape sequences which would help in outputs of our code to a great extent. A clear concept of these is of utmost importance in programming.

Your opinion matters
Please write your valuable feedback about DataFlair on Google

courses

DataFlair Team

DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.

2 Responses

  1. Supriyo Das Adhikary says:

    Thank you very much for these informations. For this information, I am able to complete my computer project. Thank you very much again

Leave a Reply

Your email address will not be published. Required fields are marked *