Java Character Class – 8 Astonishing Methods with Syntax & Examples

There are different types of classes in Java, and Character Class is one of them. Java character class wraps a value of the primitive type char in an object. There are several methods for identifying the type of character, identifying whitespace, and converting a character from uppercase to lowercase and vice versa.

What is Java Character Class?

In Java to print a character, we use primitive data types char. The character class is immutable. Java language provides a wrapper class in java.lang package. The object of character has only a single field which is a character. Once the object of the character is created, we can not change it.

Character ch = new Character('a');

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

Don’t forget to read about Wrapper Class in Java.

Character Class Methods in Java

Java Character Class Methods

1. 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 it returns 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
package com.DataFlair.CharacterClass;
public class isLetterDemo 
{
  public static void main(String[] args)
      {
       // return true if it is a letter
        System.out.println(Character.isLetter('A'));
       // returns false if it is not a letter
        System.out.println(Character.isLetter('0'));
      }
    }

Output-is-letter-demo

It’s the right time to explore Type Casting and Type Conversion in Java.

2. 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
package com.DataFlair.CharacterClass;
public class isDigitDemo {

      public static void main(String[] args)
      {
        //checks whether the 'D','1' and 'F' are digits or not
        System.out.println(Character.isDigit('D'));
        System.out.println(Character.isDigit('1'));
        System.out.println(Character.isDigit('F'));
        
      }
    }

Output-is-digit-demo

3. boolean isWhitespace(char ch)

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

Syntax

boolean isWhitespace(char ch)

Example

// Java program to demonstrate isWhitespace() method
package com.DataFlair.CharacterClass;
public class isWhiteSpace {
  public static void main(String[] args)
      {
        System.out.println(Character.isWhitespace('B'));
        System.out.println(Character.isWhitespace(' '));
        System.out.println(Character.isWhitespace(5));
        System.out.println(Character.isWhitespace('d'));
        System.out.println(Character.isWhitespace('1'));
        System.out.println(Character.isWhitespace('\n'));
          System.out.println(Character.isWhitespace('\t'));
      }
    }

Output-is-white-space

You Must Know – Java Inner Class with Example

4. boolean isUpperCase(char ch)

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

Syntax

boolean isUpperCase(char ch)

Example

// Java program to demonstrate isUpperCase() method
package com.DataFlair.CharacterClass;
public class isUpperCaseDemo {

      public static void main(String[] args)
      {
        System.out.println(Character.isUpperCase('C'));
        System.out.println(Character.isUpperCase('c'));
        //here the value in the numeric is the ASCII value of C
        System.out.println(Character.isUpperCase(67));
      }
    }

Output – is-upper-case-demo

5. boolean isLowerCase(char ch)

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

Syntax

boolean isLowerCase(char ch)

Example

// Java program to demonstrate isLowerCase() method
package com.DataFlair.CharacterClass;
  public class isLowerCaseDemo {

        public static void main(String[] args)
        {
          System.out.println(Character.isLowerCase('C'));
          System.out.println(Character.isLowerCase('c'));
          //here the value in the numeric is the ASCII value of C
          System.out.println(Character.isLowerCase(67));
        }
      }

Output-is-lower-case

Uncover the Constructors & Methods used in Java File Class

6. char toUpperCase(char ch)

char toUpperCase(char ch) method in Java is used when we want to convert the given lower case into the upper case.

Syntax

char toUpperCase(char ch)

Example

// Java program to demonstrate toUpperCase() method
package com.DataFlair.CharacterClass;

public class toUpperCase {
      public static void main(String[] args)
      {
         System.out.println(Character.toUpperCase('a'));
         //ascii value of z is 122
         System.out.println(Character.toUpperCase(122));
         //ascii value of n is 110
         System.out.println(Character.toUpperCase(110));
      }
    }

Output-to-upper-case

7. char toLowerCase(char ch)

char ToLowerCase(char ch) method in Java is used to convert the given upper case to the lower case.

Syntax

char toLowerCase(char ch)

Example

// Java program to demonstrate toLowerCase() method
package com.DataFlair.CharacterClass;
public class toLowerCase {

      public static void main(String[] args)
      {
        System.out.println(Character.toLowerCase('A'));
         //ascii value of Z is 90
        System.out.println(Character.toLowerCase(90));
         //ascii value of N is 78
        System.out.println(Character.toLowerCase(78));
      }
    }

Output-to-lower-case

Before we discuss the 8th method, you should know what is String in Java.

8. toString(char ch)

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

Syntax

String toString(char ch)

Example

// Java program to demonstrate toString()
package com.DataFlair.CharacterClass;
 public class toStringdemo {
      public static void main(String args[])
       {
    	   // the character will be printed as it is
        System.out.println(Character.toString('D'));
        System.out.println(Character.toString('f'));
     }
  }

Output-to-string-demo

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 available 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
Java Quiz

Summary

In this Java character tutorial, we learned about what is Java character class, different methods for declaring character in Java, different escape sequences in Java, which will further help us to understand the complex Java programs.

Explore Pair Class in Java With Techniques

Furthermore, if you have a query, feel free to ask in a comment box.

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.