Easy Tutorial
❮ Java String Tolowercase Java Hashmap Clone ❯

Java Character Class

The Character class is used to manipulate individual characters.

The Character class wraps a value of the primitive type char in an object.

Example

char ch = 'a';

// Unicode character representation
char uniChar = '\u039A'; 

// Array of characters
char[] charArray ={ 'a', 'b', 'c', 'd', 'e' };

However, in practical development, we often encounter situations where we need to use objects instead of built-in data types. To address this issue, the Java language provides a wrapper class, Character, for the built-in data type char.

The Character class provides a series of methods to manipulate characters. You can use the constructor of Character to create a Character class object, for example:

Character ch = new Character('a');

In some cases, the Java compiler will automatically create a Character object.

For example, when passing a char type parameter to a method that requires a Character type parameter, the compiler will automatically convert the char type parameter to a Character object. This feature is known as autoboxing, and the reverse process is called unboxing.

Example

// Primitive character 'a' is boxed into Character object ch
Character ch = 'a';

// Primitive character 'x' is boxed using the test method
// Returns the unboxed value to 'c'
char c = test('x');

Escape Sequences

A character preceded by a backslash () is an escape sequence and has a special meaning to the compiler.

The following table shows the Java escape sequences:

Escape Sequence Description
\t Inserts a tab in the text at this point.
\b Inserts a backspace in the text at this point.
\n Inserts a newline in the text at this point.
\r Inserts a carriage return in the text at this point.
\f Inserts 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 character in the text at this point.

Example

When a print statement encounters an escape sequence, the compiler can interpret it correctly.

The following example escapes a double quote and outputs:

Test.java File Code:

public class Test {

   public static void main(String[] args) {
      System.out.println("Visit \"tutorialpro.org!\"");
   }
}

The above example compiles and runs with the following result:

Visit "tutorialpro.org!"

Character Methods

The following are the methods of the Character class:

No. Method and Description
1 isLetter() <br>Determines if the specified character is a letter.
2 isDigit() <br>Determines if the specified character is a digit.
3 isWhitespace() <br>Determines if the specified character is a whitespace.
4 isUpperCase() <br>Determines if the specified character is an uppercase letter.
5 isLowerCase() <br>Determines if the specified character is a lowercase letter.
6 toUpperCase() <br>Returns the uppercase form of the specified character.
7 toLowerCase <br>Returns the lowercase form of the specified character.
8 toString <br>Returns a String object representing the specified character. The length of the string is 1.

For a complete list of methods, please refer to the java.lang.Character API specification.

❮ Java String Tolowercase Java Hashmap Clone ❯