Mega Code Archive

 
Categories / Java Book / 001 Language Basics
 

0020 char Literals

Characters in Java are indices into the Unicode character set. character is represented inside a pair of single quotes. For example, 'a', 'z', and '@'. public class Main { public static void main(String[] argv) { char ch = 'a'; System.out.println("ch is " + ch);//ch is a } } public class Main { public static void main(String[] argv) { char ch = '@'; System.out.println("ch is " + ch);//ch is @ ch = '#'; System.out.println("ch is " + ch);//ch is # ch = '$'; System.out.println("ch is " + ch);//ch is $ ch = '%'; System.out.println("ch is " + ch);//ch is % } } The escape sequences are used to enter impossible-to-enter-directly characters. '\'' is for the single-quote character. '\n' for the newline character. For octal notation, use the backslash followed by the three-digit number. For example, '\141' is the letter 'a'. For hexadecimal, you enter a backslash-u (\u), then exactly four hexadecimal digits. For example, '\u0061' is the ISO-Latin-1 'a' because the top byte is zero. '\ua432' is a Japanese Katakana character. The following table shows the character escape sequences. Escape Sequence Description \ddd Octal character (ddd) \uxxxx Hexadecimal Unicode character (xxxx) \' Single quote \" Double quote \\ Backslash \r Carriage return \n New line \f Form feed \t Tab \b Backspace public class Main { public static void main(String[] argv) { char ch = '\''; System.out.println("ch is " + ch);//ch is ' } }