Java Character Class Methods with Syntax and Examples

1. Objective

In this java character Class tutorial, we are going to learn about the different characters used in the Java Programming language, how to create Java character Class, different methods to declare characters in Java.

So, let’s Java Character Class Tutorial.

Java Character - Introduction

Java Character Class Methods with Syntax and Examples

2. Java Character

Java language provides a wrapper java class in java.lang package, it contains a single which is of the type char.

Character ch = new Character('a');

The line above creates a Java character ‘a’ which is of the type char.

Do you know What is Java Inner Class?

3. Character Class Methods in Java

Java Character Class Methods

Character Class Methods In Java

a. Java boolean isLetter (char ch)

A boolean isLetter method is used in Java, for checking whether the given char value(ch) is a letter or not, i.e. [A-Z],[a-z], it will return true if it is a letter else false.

We can also write ASCII value for the letter because Java has an implicit typecasting from char to int.

Syntax

boolean isLetter(char ch)

Example

// Java program to demonstrate isLetter() method
public class Test
  {
    public static void main(String[] args)
    {
      System.out.println(Character.isLetter('A'));
      System.out.println(Character.isLetter('0'));
    }
  }

Output
true
false

b. Java boolean isDigit(char ch)

Boolean isDigit method in Java is used to determine whether the given character is a digit or not.
Syntax

boolean isDigit(char ch)

Example

// Java program to demonstrate isDigit() method
public class Test
  {
    public static void main(String[] args)
    {
      // print false as A is character
      System.out.println(Character.isDigit('A'));
      System.out.println(Character.isDigit('0'));
    }
  }

Read more about Java Operators

c. Java boolean is Whitespace(char ch)

A whitespace in Java is used as a space, tab, or a new line, and this method determines whether the given char is a whitespace or not.
Syntax

boolean isWhitespace(char ch)

Example

// Java program to demonstrate isWhitespace() method
public class Test
  {
    public static void main(String[] args)
    {
      System.out.println(Character.isWhitespace('A'));
      System.out.println(Character.isWhitespace(' '));
      System.out.println(Character.isWhitespace('\n'));
            System.out.println(Character.isWhitespace('\t'));
      //ASCII value of tab
      System.out.println(Character.isWhitespace(9));
      System.out.println(Character.isWhitespace('9'));
    }
  }

Output
false
true
true
true
true
false

d. Java Boolean isUpperCase(char ch)

Boolean isUpperCase method in Java is used, to determine whether the given char value is uppercase or not.
Syntax

boolean isUpperCase(char ch)

Example

// Java program to demonstrate isUpperCase() method
public class Test
  {
    public static void main(String[] args)
    {
      System.out.println(Character.isUpperCase('A'));
      System.out.println(Character.isUpperCase('a'));
      System.out.println(Character.isUpperCase(65));
    }
  }

Output – True
False
True

Have you know – Encapsulation in Java?

e. Java boolean isLowerCase(char ch)

Boolean isLowerCase(char ch) method in Java is used to determine whether the given char value is lowercase or not.
Syntax

boolean isLowerCase(char ch)

Example

// Java program to demonstrate isLowerCase() method
public class Test
  {
    public static void main(String[] args)
    {
 		  System.out.println(Character.isLowerCase('A'));
      System.out.println(Character.isLowerCase('a'));
      System.out.println(Character.isLowerCase(97));
    }
  }

Output
false
true
true

f. Java char toUpperCase(char ch)

Char toUpperCase(char ch) method in Java  is used when the uppercase of the given java character is needed.
Syntax

char toUpperCase(char ch)

Example

// Java program to demonstrate toUpperCase() method
public class Test
  {
    public static void main(String[] args)
    {
       System.out.println(Character.toUpperCase('a'));
       System.out.println(Character.toUpperCase(97));
       System.out.println(Character.toUpperCase(48));
    }
  }

Output
A
65
48

Follow this link to know about Loops in Java

g. Java char ToLowerCase(char ch)

Char ToLowerCase(char ch) method in Java is used when the lowercase of the given java char value is required.
Syntax

char toLowerCase(char ch)

Example

// Java program to demonstrate toLowerCase() method
public class Test
  {
    public static void main(String[] args)
    {
      System.out.println(Character.toLowerCase('A'));
      System.out.println(Character.toLowerCase(65));
      System.out.println(Character.toLowerCase(48));
    }
  }

Output
a
97
48

h. Java toString(char ch)

In toString(char ch) method in Java, a string class object is returned for the specified char. We cannot use ASCII value here.
Syntax

String toString(char ch)

Example

// Java program to demonstrate toString()
 method public class Test
 {
           public static void main(String[] args)
             { 
                System.out.println(Character.toString('x'));
                System.out.println(Character.toString('Y'));
             }
 }

Output
X
y

Read about Java Modifiers.

Java Quiz

4. Java Escape Sequences

An escape sequence in Java is a character with a backslash (\), it holds a special meaning to the compiler. The following are escape sequences used in Java

Escape SequenceDescription
\tTo insert a tab
\bTo insert a backspace
\nTo insert a newline
\rTo insert a carriage return
\fTo insert a form feed
\’To insert a single quote character
\”To insert a double quote character
\\To insert a backslash character

So, this was all about Java Character Class. Hope you like our explanation.

5. Conclusion

In this tutorial for Java character, we learned about what is Java character, different methods for declaring character in Java, different escape sequences in Java, which will further help us to understand the complex Java programs. Furthermore, if you have a query, feel free to ask in a comment box.

See Also – Inheritance in Java 

For reference

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.