Java Characters
Normally, when we work with characters, we use primitive data types char. When we have to work with the objects of char, we use Character class.
Character class has many important methods to manipulate characters.
Example:
char primitive is created by
char ch = ‘A’;
Character object is created as
Character ch = new Character('A')
The compiler converts the primitive char type to Character object as required, like when we pass a char to a method and the method accepts Character object then it converts it to character object.
Escape Sequences
(/) is called escape sequence and the compiler treats this character especially, it is used in different scenarios like:-
\t It inserts a tab in the text at this point.
\b It inserts a backspace in the text at this point.
\n It inserts a newline in the text at this point.
\r It inserts a carriage return in the text at this point.
\f It inserts a form feed in the text at this point.
\' It inserts a single quote character in the text at this point.
\" It inserts a double quote character in the text at this point.
\\ It inserts a backslash character in the text at this point.
Example:
public class EscapeCharExample { public static void main(String args[]) { System.out.println("\"Hi\""); } }
Output:
"Hi"
Java Character Methods
isLetter(): It determines whether the specified char value is a letter.
isDigit(): It determines whether the specified char value is a digit.
IsWhitespace(): It determines whether the specified char value is white space.
IsUpperCase(): It determines whether the specified char value is uppercase.
isLowerCase(): It determines whether the specified char value is lowercase.
toUpperCase(): It returns the uppercase form of the specified char value.
toLowerCase() : It returns the lowercase form of the specified char value.
toString(): It returns a string object representing the specified character value that is, a one character string.