Character in Java
In Java, a character is represented by the char data type. The char type is a primitive data type that represents a single 16-bit Unicode character. It can hold any character from the Unicode character set, including letters, digits, symbols, and whitespace.
Here's an example of declaring and initializing a char variable in Java:
char firstChar = 'K';
In the above example, the variable firstChar is declared as a char and assigned the value 'K', which represents the character 'K'.
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.
Here is an example, where the variable firstChar is assigned the code point \u004B, which represents the character 'K' in Unicode:
char firstChar = '\u004B';
Example:
public class Sample { public static void main(String[] args) { char firstChar = '\u004B'; System.out.println(firstChar); } }
Output:
K
Character Class
Java provides a Character class in the java.lang package. The Character class is a wrapper class that wraps the primitive char data type and provides a Character object. A Character object contains a single field of char. This Character class also provides a number of useful class methods (static methods) and constants for working with characters. The Character class is immutable, which means once it is created, it cannot be changed.
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