// some basics of characters public class chars0 { public static void main(String[] args) { // Characters use the primitive type $char$: char value = 'a'; // Characters are NOT equivalent to the values inside the $''$: char x = '0'; if (x == 0) System.out.println("That shouldn't have happened."); // Character codes are also integers: System.out.println((int) value); // the letter 'a' is code 97 // To go from $int$ to $char$, use a $char$ cast: System.out.println((char) 97); // the letter 'a' is code 97 // When operating on $char$s and $int$s, Java promotes the quantity // to $int$: System.out.println('a'+1); // Characters can use the ASCII numbering system (0 to 127): // "printing" characters (keyboard characters): System.out.println(); for(int i=32;i<126;i++) System.out.print((char) i); // Sometimes you may print out "extended ASCII": System.out.println(); for(int i=128;i<257;i++) System.out.print((char) i); // Example of "nonprinting" character (the bell): System.out.println((char) 7); // There is no "empty" character: // char z = ''; // illegal! // But, there is a character called NUL: System.out.println( (char) 0 ); // doesn't do much // This value is also the default value of a $char$ instance variable. // Handy trick for converting upper and lower cases: // 'A' = 65, 'a' = 97 --> 'a'-'A'=32 System.out.println( (char)( 'd' - ('a'-'A')) ); // convert to uppercase } } /* output: 97 a 98 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_` abcdefghijklmnopqrstuvwxyz{|}  ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæ çèéêëìíîïðñòóôõö÷øùúûüýþÿ? D */