In Java, a character literal is a single character enclosed in single quotes, like this:
char ch = 'A';
Character literals can represent any Unicode character, including letters, digits, and symbols. You can also use escape sequences to represent special characters in a character literal. For example:
char newline = '\n'; // represents a newline character char tab = '\t'; // represents a tab character char backslash = '\\'; // represents a backslash character
In Java, character literals are of the char
data type, which is a primitive type that represents a single Unicode character. The char
data type is a 16-bit unsigned integer, and it can store any Unicode character in the range '\u0000'
(or 0
) to '\uffff'
(or 65535
).
You can use character literals in various ways in your Java programs. For example, you can use them to initialize char
variables, like this:
char ch = 'A';
You can also use character literals in string literals, which are sequences of characters enclosed in double quotes. For example:
String str = "The character is 'A'";
In this example, the character literal 'A'
is part of the string literal.
You can also use character literals in character arrays, like this:
char[] chars = {'A', 'B', 'C'};
This will create an array of char
values that represents the characters 'A'
, 'B'
, and 'C'
.