Understanding Character in Java

  • Last updated Apr 25, 2024

In Java, a character is a primitive data type represented by the char data type. The character value is enclosed in single quotes, and it is a single 16-bit Unicode character that can hold any character from the Unicode character set, including letters, digits, symbols, and whitespace.

Declaring and Initializing a Character

Here's how to declare and initialize a character using a literal in Java:

char letter = 'A';

In the above example, 'A' is a character literal assigned to the variable letter.

The Character Class

Java provides a wrapper class called Character in the java.lang package for the char data type. This class offers several utility methods and constants to perform various operations on characters.

Converting between Primitive char and Object Character

Here's how you can convert between char and Character:

char ch = 'A';
Character character = Character.valueOf(ch); // Boxing
char convertedChar = character.charValue();   // Unboxing
Useful Methods in the Character Class

Here are some commonly used methods of the Character class:

Character Method Description
boolean isLetter(char ch)
Determines if the specified char value is a letter.
boolean isDigit(char ch)
Determines if the specified char value is a digit.
boolean isUpperCase(char ch)
Determines if the specified char value is uppercase.
boolean isLowerCase(char ch)
Determines if the specified char value is lowercase.
char toUpperCase(char ch)
Returns the specified char value in uppercase form.
char toLowerCase(char ch)
Returns the specified char value in lowercase form.
String toString(char ch)
Returns the specified character value in a String object — that is, a one-character string.
boolean isWhitespace(char ch)
Determines if the specified char value is white space.
boolean isLetterOrDigit(char ch)
Determines if the specified character is a letter or digit.
boolean isSpaceChar(char ch)
Determines if the specified character is a space character.

Additionally, the Character class also provides constants such as MIN_VALUE, MAX_VALUE, MIN_RADIX, and MAX_RADIX, which represent the minimum and maximum values that can be assigned to a char type, as well as the minimum and maximum radix values for character conversion methods.

Here's an example that shows how to use methods from the Character class:

public class Sample {

   public static void main(String[] args) {

	char ch = 'K';

	System.out.println(Character.isLetter(ch));
	System.out.println(Character.isDigit(ch));
	System.out.println(Character.isUpperCase(ch));
	System.out.println(Character.isLowerCase(ch));
	System.out.println(Character.isWhitespace(ch));
	System.out.println(Character.isLetterOrDigit(ch));
	System.out.println(Character.isSpaceChar(ch));

	char lowerCaseCh = Character.toLowerCase(ch);
	System.out.println(lowerCaseCh);
	char upperCaseCh = Character.toUpperCase(ch);
	System.out.println(upperCaseCh);
	String chString = Character.toString(ch);
	System.out.println(chString);

   }
}

The output of the above code is as follows:

true
false
true
false
false
true
false
k
K
K
Using Unicode Code Point

You can also assign a character using its Unicode code point. The Unicode code point is a unique number assigned to each character in the Unicode standard. To represent a character using its code point, you can use the escape sequence \u followed by the four-digit hexadecimal representation of the code point.

Example:

To represent the character 'A' using its Unicode code point, which is U+0041 in hexadecimal:

char characterA = '\u0041';
System.out.println(characterA);  // This will print: A

In the example above, the escape sequence \u0041 is used to assign the character 'A' to the variable characterA.