Java Characters - Complete Guide
Introduction to Character Data Type
The char data type in Java represents a single 16-bit UTF-16 code unit. It can directly store characters from the Basic Multilingual Plane (BMP). Characters outside the BMP (code points above U+FFFF) are represented using a surrogate pair (two char values).
Java relies on Unicode, which supports writing systems and symbols from many languages, making it suitable for global applications. Understanding char as a code unit and its relationship to code points is essential for advanced text processing.
Character Declaration and Usage
Characters can be declared with single quotes, Unicode escapes, or by assigning numeric values within the valid range.
public class CharacterBasics {
public static void main(String[] args) {
char letter = 'A';
char digit = '7';
char symbol = '$';
char space = ' ';
char newline = '\n';
System.out.println("Letter: " + letter);
System.out.println("Digit: " + digit);
System.out.println("Symbol: " + symbol);
System.out.println("Space: " + space + "(invisible)");
System.out.println("Newline: \\n(invisible)");
char omega = '\u03A9';
char heart = '\u2764';
char chinese = '\u4E2D';
System.out.println("Omega: " + omega);
System.out.println("Heart: " + heart);
System.out.println("Chinese: " + chinese);
char a = 'a';
char nextLetter = (char)(a + 1);
System.out.println("Next letter after 'a': " + nextLetter);
CharWrapper wrapper = new CharWrapper();
System.out.println("Default char: " + (int)wrapper.defaultChar + " (numeric code)");
}
}
class CharWrapper {
char defaultChar; // Defaults to '\u0000' (null character)
}
Letter: A Digit: 7 Symbol: $ Space: (invisible) Newline: (invisible) Omega: Ω Heart: ❤ Chinese: 中 Next letter after 'a': b Default char: 0 (numeric code)
Character Methods and Utilities
The Character class provides methods to test, convert, and compare char values. These utilities are essential for validating and transforming characters.
public class CharacterMethods {
public static void main(String[] args) {
char ch1 = 'A';
char ch2 = '5';
char ch3 = ' ';
char ch4 = 'a';
System.out.println("Is 'A' letter? " + Character.isLetter(ch1));
System.out.println("Is '5' digit? " + Character.isDigit(ch2));
System.out.println("Is ' ' whitespace? " + Character.isWhitespace(ch3));
System.out.println("Is 'A' uppercase? " + Character.isUpperCase(ch1));
System.out.println("Is 'a' lowercase? " + Character.isLowerCase(ch4));
System.out.println("Uppercase of 'a': " + Character.toUpperCase(ch4));
System.out.println("Lowercase of 'A': " + Character.toLowerCase(ch1));
System.out.println("Numeric value of '5': " + Character.getNumericValue(ch2));
System.out.println("Code point of 'A': " + (int)ch1);
System.out.println("Minimum value: " + (int)Character.MIN_VALUE);
System.out.println("Maximum value: " + (int)Character.MAX_VALUE);
System.out.println("Size in bits: " + Character.SIZE);
System.out.println("Compare 'A' and 'a': " + Character.compare(ch1, ch4));
System.out.println("Are 'A' and 'a' equal? " + (ch1 == ch4));
}
}
Is 'A' letter? true Is '5' digit? true Is ' ' whitespace? true Is 'A' uppercase? true Is 'a' lowercase? true Uppercase of 'a': A Lowercase of 'A': a Numeric value of '5': 5 Code point of 'A': 65 Minimum value: 0 Maximum value: 65535 Size in bits: 16 Compare 'A' and 'a': -32 Are 'A' and 'a' equal? false
Working with Character Arrays
Character arrays are often used for direct string manipulation and for security-sensitive text handling, since they can be cleared after use.
public class CharacterArrays {
public static void main(String[] args) {
char[] vowels = {'a', 'e', 'i', 'o', 'u'};
char[] message = "Hello World".toCharArray();
char[] empty = new char[5];
System.out.println("First vowel: " + vowels[0]);
vowels[0] = 'A';
System.out.println("Modified first vowel: " + vowels[0]);
System.out.print("Vowels: ");
for (char vowel : vowels) {
System.out.print(vowel + " ");
}
System.out.println();
String strFromArray = new String(message);
System.out.println("String from array: " + strFromArray);
char[] reversed = new char[message.length];
for (int i = 0; i < message.length; i++) {
reversed[message.length - 1 - i] = message[i];
}
System.out.println("Reversed: " + new String(reversed));
java.util.Arrays.fill(empty, '*');
System.out.println("Filled array: " + new String(empty));
}
}
First vowel: a Modified first vowel: A Vowels: A e i o u String from array: Hello World Reversed: dlroW olleH Filled array: *****
Special Characters and Escape Sequences
Escape sequences allow the use of special characters in string and char literals. They are essential for representing control characters and symbols that cannot be typed directly.
public class EscapeSequences {
public static void main(String[] args) {
System.out.println("Newline example:\nSecond line");
System.out.println("Tab example: Col1\tCol2");
System.out.println("Backslash: C:\\Users\\Name");
System.out.println("Double quote: \"Hello\"");
System.out.println("Single quote: It\'s fine");
System.out.println("Backspace: ABC\bD");
System.out.println("Carriage return: Start\rEnd");
char octalChar = '\141';
char hexChar = '\u0061';
System.out.println("Octal 'a': " + octalChar);
System.out.println("Hex 'a': " + hexChar);
System.out.println("Is newline whitespace? " + Character.isWhitespace('\n'));
System.out.println("Is tab whitespace? " + Character.isWhitespace('\t'));
}
}
Newline example: Second line Tab example: Col1 Col2 Backslash: C:\Users\Name Double quote: "Hello" Single quote: It's fine Backspace: ABD Carriage return: Endrt Octal 'a': a Hex 'a': a Is newline whitespace? true Is tab whitespace? true