logo

Java Character Class


Show

In general, while we effort with characters, we employ primitive data types char.

Example

char ch = 'a';

// Unicode for uppercase Greek omega character
char uniChar = '\u039A'; 

// an array of chars
char[] charArray ={ 'a', 'b', 'c', 'd', 'e' }; 

However, in progress, we encounter situations where we want to exercise objects instead of ancient data types. In order to realize this, Java supplies wrapper class Character for ancient data type char.

The Character class offers a number of useful class (i.e., static) methods for manipulating characters. You can create a Character object with the Character constructor:

Character ch = new Character('a');

The Java compiler will too make a Character object for you beneath some conditions. For instance, if you bypass a prehistoric char into a technique that anticipates an object, the compiler mechanically converts the char to a Character for you. This characteristic is called autoboxing or unboxing if the change goes the other method.

Example

// Here following primitive char 'a'
// is boxed into the Character object ch
Character ch = 'a';

// Here primitive 'x' is boxed for method test,
// return is unboxed to char '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 newline character (\n) has been used frequently in this tutorial in System.out.println() statements to advance to the next line after the string is printed.

The following table shows the Java escape sequences −

Escape SequenceDescription
\tInserts a tab in the text at this point.
\bInserts a backspace in the text at this point.
\nInserts a newline in the text at this point.
\rInserts a carriage 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 character in the text at this point.

When an escape sequence is encountered in a print statement, the compiler interprets it accordingly.

Example

If you want to put quotes within quotes, you must use the escape sequence, \", on the interior quotes −

public class Test {

   public static void main(String args[]) {
      System.out.println("She said \"Hello!\" to me.");
   }
}

This will produce the following result:

Output

She said "Hello!" to me.

Character Methods

Following is the list of the important instance methods that all the subclasses of the Character class implement −

Sr.No.Method & Description
1isLetter()

Determines whether the specified char value is a letter.

2isDigit()

Determines whether the specified char value is a digit.

3isWhitespace()

Determines whether the specified char value is white space.

4isUpperCase()

Determines whether the specified char value is uppercase.

5isLowerCase()

Determines whether the specified char value is lowercase.

6toUpperCase()

Returns the uppercase form of the specified char value.

7toLowerCase()

Returns the lowercase form of the specified char value.

8toString()

Returns a String object representing the specified character value that is, a one-character string.

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

Here at Intellinuts, we have created a complete Java tutorial for Beginners to get started in Java.