Java for Everyone... character
In Java, a Character in a string refers to an individual element of the string, so if you are using a single character value, The character must be surrounded by single quotes, like 'A' ,’1’, '@' etc.
A char is a primitive data type, used to represent a single 16-bit Unicode character. It can hold any character, such as a letter, a digit, or a special symbol, from the Unicode character set..
Each character has a unique code point, ranging from 0 to 1,114,112 (or up to 10FFFF in hexadecimal)
For example:
·?????? "A" is represented by the code point U+0041.
·?????? "B" is represented by the code point U+0042.
·?????? "a" is represented by the code point U+0061.
·?????? "b" is represented by the code point U+0062.
·?????? "??" (smiling face) is represented by U+1F60A.
·?????? ?"(Ω)" Omega is represented by ?U+03A9
Variable declaration
char ch=’A’;
char ch = 'a';
char uniChar = '\u03A9';
char[] charArray = { 'a', 'b', 'c', 'd', 'e' };
You need to use char as object as method argument where an object is expected, Java provides a wrapper class that "wraps" the char in a Character object for this purpose.
An object of type Character contains a single field, whose type is char.
This Character class also offers several useful class (that is, static) methods for manipulating characters, you can create a Character object with the Character constructor.
Character ch = new Character('a');
Here, if you pass a primitive char into a method that expects an object, the compiler automatically converts the char to a Character.
The Character class is immutable, so that once it is created, a Character object cannot be changed.
Useful Methods in the Character Class
boolean isLetter(char ch)-->Determines whether the specified char value is a letter
boolean isDigit(char ch)?--> Determines whether the specified char value is a digit
boolean isWhitespace(char ch)--> Determines whether the specified char value is white space.
boolean isUpperCase(char ch)-->Determines whether the specified char value is uppercase
boolean isLowerCase(char ch)?-->Determines whether the specified char value is lowercase
char toUpperCase(char ch)-->Returns the uppercase form of the specified char value.
char toLowerCase(char ch)--> Returns the lowercase form of the specified char value.
toString(char ch)?-->Returns a String object representing the specified character value — that is, a one-character string.
Escape Sequences
A character preceded by a backslash (\) is an escape sequence and has special meaning to the compiler. The following table shows the Java escape sequences:
Escape Sequence????? Description
\t?????????? Insert a tab in the text at this point.
\b????????? Insert a backspace in the text at this point.
\n????????? Insert a newline in the text at this point.
领英推荐
\r?????????? Insert a carriage return in the text at this point.
\f?????????? Insert a form feed in the text at this point.
\'??????????? Insert a single quote character in the text at this point.
\"?????????? Insert a double quote character in the text at this point.
\\?????????? Insert 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-
public static void main(String[] args) {
char letter = '\u03A9';
//letter;
System.out.println(letter);
}
Key Characteristics of Char
We know In Java, the char data type represents a single 16-bit Unicode character,? here are the key characteristics -
1.??Size: A char in Java is a 16-bit (2-byte) value, which can represent any Unicode character from U+0000 to U+FFFF.
2.??Unicode Support: It stores characters in Unicode, allowing it to represent characters from various writing systems, including alphabets, symbols, and emojis. This enables Java to support internationalization and work with different languages.
3.?Type: The char type is a primitive data type in Java and is not considered a number type, even though it can be treated as an integer in some contexts (e.g., arithmetic operations on char will give the corresponding numeric values based on Unicode code points).
4.??Literal Representation: A char value is enclosed in single quotes ('A', '1', '$', etc.). For characters outside the ASCII range, escape sequences like \uXXXX (where XXXX is the hexadecimal Unicode code) are used (e.g., '\u03A9' for Ω).
5.??Range: Since char is a 16-bit value, it can store a range of characters with Unicode values from 0 to 65535 (i.e., Character.MIN_VALUE to Character.MAX_VALUE).
6.??Char Arithmetic: You can perform arithmetic operations on char values because they are internally represented as integers (based on their Unicode code points). For example, 'A' + 1 results in 'B'.
7.??Default Value: The default value of a char in Java is \u0000 (null character).
8.???Special Characters: char can also represent special characters like newline (\n), tab (\t), and carriage return (\r).
Food for thought
Why Char is case sensitive?
What is the default value of a char variable in Java?
How do you represent a char using its Unicode value?
Can you perform arithmetic operations on a char?
What happens when you add two char values?
How do you convert a char to its integer equivalent?
How do you increment or decrement a char?
What is the Unicode value of the null character (\u0000)?
How do you find the numeric Unicode code point of a char?
/*
This is article is based on JDK 8, any changes on features, enhancement or deprecated options can be referred to official release notes-
*/