Java Character Class – Implement Methods with Coding Examples

We know that there are 8 primitive data types in Java. Java’s primitive data types are the data values and not objects. Java Character Class wraps the value of Primitive data type in an object. There are some cases when we may encounter a situation where numerical values are needed but in the form of objects. Java solves this problem by providing the concept of wrapper classes.

Wrapper classes are part of Java’s standard library java.lang and these convert primitive data type into an object.

Java provides 8 wrapper classes which are:

DatatypeWrapper Class
booleanBoolean
byteByte
charCharacter
shortShort
intInteger
longLong
floatFloat
doubleDouble

Wait! first, get a deep insight into Java Classes with Techvidhvan. 

Java Character Class

In this article, we will discuss the Character Class in Java which wraps the value of primitive data type char into its object. All the attributes, methods, and constructors of the Character class are specified by the Unicode Data file which is maintained by the Unicode Consortium. The instances or objects of Character class can hold single character data. In addition, this wrapper class also provides several methods useful when manipulating, inspecting or dealing with the single-character data.

Syntax of creating an object from the Character class is as follows:

Character letter = new Character( 'g' );
Character num = new Character( '7' );

We created the object of the Character wrapper class using the Character constructor. In the above syntax, the Java compiler will automatically convert the ‘char’ value into an object of Character type. This process of converting primitive data type into an object is called autoboxing and, the reverse process, that is, converting the object into primitive data types is called unboxing.

The Character Class is immutable, which means, once we create its object, we cannot change it.

The above syntax can also be written like this:

Character char1 = new Character( 'g' );
// primitive char 'g' is wrapped in a Character object char1.
Character char2 = new Character( char1 );
// primitive value of char char1 is wrapped in a Character object char2.

Escape Sequences

An escape sequence is a character preceded by a backslash(\), which gives a different meaning to the compiler. The following table shows the escape sequences in Java:

Escape SequenceDescription
\tInserts a tab in the text at this point.
\bInserts a backspace in the text at this point.
\nInserts a new line in the text at this point
\rInserts a carrier return in the text at this point
\fInserts a form feed in the text at this point
\’Inserts a single quote character in the text at this point
\”Inserts a double quote character in the text at this point
\\Inserts a backslash in the text at this point

Code to illustrate some escape sequences:

package com.TechVidvan.CharacterDemo;
public class EscapeSequencesDemo
{
  public static void main(String args[])
  {
    System.out.print("Hello \nWelcome to"); //using \n
    System.out.println(" The \"TechVidvan\" tutorial."); //using \"
    System.out.println("This is a \'Java\' tutorial."); //using \'
    System.out.println("My java file is in: projects\\src\\java"); //using \\
  }
}

Output:

Hello
Welcome to The “TechVidvan” tutorial.
This is a ‘Java’ tutorial.
This is a “TechVidvan” tutorial.
My java file is in: projects\src\java

Methods of Character Class in Java

The Character Class comes with several methods that are useful for performing operations on characters. These methods are static in nature, that is, they can be directly called with the help of class name without creating any object. We will discuss each of these methods in detail:

Java Character Class Methods

1. static boolean isDigit(char ch) Method

The method isDigit() is used to determine whether the specific character value (ch) is a digit or not. It checks whether the value is a digit that is 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.

As it is a boolean method, it returns true if the character is a digit and false if character value is not a digit.

Code to illustrate the use of the isDigit() method:

package com.TechVidvan.CharacterDemo;
public class IsDigitMethod
{
  // Java program to demonstrate isDigit() method
  public static void main(String[] args)
  {
    char ch = 'A';
    char ch1 = '1';
    //checks whether the values of ch and cha1 are digits or not.
    System.out.println(Character.isDigit(ch));
    System.out.println(Character.isDigit(ch1));
    //checks whether the values ‘t’ , ‘8’, ’ H’ are digits or not.
    System.out.println(Character.isDigit('t'));
    System.out.println(Character.isDigit('8'));
    System.out.println(Character.isDigit('H'));
  }
}

Output:

false
true
false
true
false

2. static boolean isLetter(char ch) Method

The method isLetter() is used to determine whether the specific character value (ch) is a letter or not. It checks whether the value is a letter that is, [ A – Z ] or [ a – z ]. As it is a boolean method, it returns true if the character is a letter and false if character value is not a letter.

We can also write the ASCII value of the letter because Java can implicitly typecast the value from char to int.

Code to illustrate the use of the isLetter() method:

package com.TechVidvan.CharacterDemo;
public class IsLetterMethod
{
  // Java program to demonstrate isLetter() method
  public static void main(String[] args)
  {
    char ch = 65; //Passing an ASCII value 65 which is equal to ‘A’
    char ch1 = '3';

    //checks whether the values of ch and ch1 are letters or not
    System.out.println(Character.isLetter(ch));
    System.out.println(Character.isLetter(ch1));

    //checks whether the 'b', '8' and 'H' are letters or not
    System.out.println(Character.isLetter('b'));
    System.out.println(Character.isLetter('8'));
    System.out.println(Character.isLetter('H'));
  }
}

Output:

true
false
true
false
true

3. static boolean isWhiteSpace(char ch) Method

Whitespace in Java can be considered as space, tab, or a new line, and the method isWhiteSpace() determines whether the given char(ch) is whitespace or not. As it is also a boolean method, it returns true if the character is whitespace and false if character value is not whitespace.

Code to illustrate the use of the isWhiteSpace() method:

package com.TechVidvan.CharacterDemo;
public class IsWhiteSpaceMethod
{
  // Java program to demonstrate isWhitespace() method

  public static void main(String[] args)
  {
    System.out.println(Character.isWhitespace('W'));
    System.out.println(Character.isWhitespace(' '));
    System.out.println(Character.isWhitespace(0));
    System.out.println(Character.isWhitespace('t'));
    System.out.println(Character.isWhitespace('\n'));
    System.out.println(Character.isWhitespace('\t'));
    System.out.println(Character.isWhitespace('\b'));
  }
}

Output:

false
true
false
false
true
true
false

4. static boolean isUpperCase(char ch) Method

The method isUpperCase() is used to determine whether the specific character value (ch) is an uppercase letter or not. It checks whether the value is a letter that is, [ A – Z ]. As it is a boolean method, it returns true if the character is in uppercase or capital letter and false if character value is not an uppercase letter.

Code to illustrate the use of the isUpperCase() method:

package com.TechVidvan.CharacterDemo;
public class IsUpperCase
{
  // Java program to demonstrate isUpperCase() method

  public static void main(String[] args)
  {
    char ch = 78;
    //here the value in the numeric is the ASCII value of N
    System.out.println(Character.isUpperCase(ch));

    //checks whether 'B' and 'b' are in uppercase or not
    System.out.println(Character.isUpperCase('B'));
    System.out.println(Character.isUpperCase('b'));
  }
}

Output:

true
true
false

5. static boolean isLowerCase(char ch) Method

The method isLowerCase() is used to determine whether the specific character value (ch) is a lowercase letter or not. It checks whether the value is a letter that is, [ a – z ]. As it is a boolean method, it returns true if the character is in lowercase or a small letter and false if character value is not a lowercase letter.

Code to illustrate the use of the isLowerCase() method:

package com.TechVidvan.CharacterDemo;
public class IsLowerCase
{
  // Java program to demonstrate isUpperCase() method

  public static void main(String[] args)
  {
    char ch = 78;
    //here the value in the numeric is the ASCII value of N
    System.out.println(Character.isLowerCase(ch));

    //checks whether 'f', 'B' and 'b' are in Lowercase or not
    System.out.println(Character.isLowerCase('f'));
    System.out.println(Character.isLowerCase('B'));
    System.out.println(Character.isLowerCase('b'));
  }
}

Output:

false
true
false
true

6. static char toUpperCase(char ch) Method

The method toUpperCase() is used to convert the specific character value (ch) into an uppercase letter. It returns the uppercase form of the input char value.

Code to illustrate the use of the toUpperCase() method:

package com.TechVidvan.CharacterDemo;
public class ToUpperCase
{
  // Java program to demonstrate toUpperCase() method
  public static void main(String[] args)
  {
    char ch = 122;		//ASCII value of z is 122
    char ch1 = 108;		//ASCII value of l is 108

    System.out.println(Character.toUpperCase(ch));
    System.out.println(Character.toUpperCase(ch1));

    System.out.println(Character.toUpperCase('a'));
    System.out.println(Character.toUpperCase('t'));
    System.out.println(Character.toUpperCase('S'));
  }
}

Output:

Z
L
A
T
S

7. static char isLowerCase(char ch) Method

The method toLowerCase() is used to convert the specific character value (ch) into a lowercase letter. It returns the lowercase form of the input char value.

Code to illustrate the use of the toLowerCase() method:

package com.TechVidvan.CharacterDemo;
public class ToLowerCase
{
  // Java program to demonstrate toLowerCase() method
  public static void main(String[] args)
  {
    char ch = 66; //ASCII value of B is 66
    char ch1 = 90; //ASCII value of Z is 90

    System.out.println(Character.toLowerCase(ch));
    System.out.println(Character.toLowerCase(ch1));

    System.out.println(Character.toLowerCase('A'));
    System.out.println(Character.toLowerCase('R'));
    System.out.println(Character.toLowerCase('e'));
  }
}

Output:

b
z
a
r
e

8. static String toString(char ch) Method

The toString(char ch) method in Java returns an object of String class for the specified char value. In simple words, it converts a char value into String. We cannot use ASCII value in this method because this method is of String type and the ASCII value cannot be converted to the character value directly.

Code to illustrate the use of the toString() method:

package com.TechVidvan.CharacterDemo;
public class ToStringMethod
{
  // Java program to demonstrate toString() method
  public static void main(String args[])
  {
    char ch = 'R';
    // the character will be printed as it is
    System.out.println(Character.toString(ch));
    System.out.println(Character.toString('A'));
    System.out.println(Character.toString('b'));
    System.out.println(Character.toString('C'));
  }
}

Output:

R
A
b
C

Summary

Character is one of the most basic units of data in Java. It is a primitive data type that we can convert into the objects of its respective wrapper class called the Character Class. In this tutorial, we discussed the Java Character Class and different methods of the Character class which is useful to govern the character values. Along with this, we also learned different escape sequences in Java. This article will definitely help you to understand and develop the complex Java programs.

Thank you for reading our article. Don’t forget to share your feedback through the comment section below.

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.