//This is a program to show that character variables can be manipulated //like integers (addition, multiplication, division). The type case rules //are similar to that of byte and short. Read carefully the comments //given below class character_example1 { public static void main(String args[]) { char a = 'a'; int unicode_a = 'a'; char z = 'z'; int unicode_z = 'z'; char zero = '0'; int unicode_zero = zero; char A = 'A'; int unicode_A = 'A'; char Z = 'Z'; int unicode_Z = 'Z'; System.out.println(a+" has unicode "+unicode_a); System.out.println(z+" has unicode "+unicode_z); System.out.println(zero+" has unicode "+unicode_zero); System.out.println(A+" has unicode "+unicode_A); System.out.println(Z+" has unicode "+unicode_Z); System.out.println("-----------------------------------"); char c = 97; // Note that any integer literal less than 65536 // correspond to a unique Unicode character. So you can assign it to // character variable without EXPLICIT type cast. But for higher values // you will get comilation error. // Recall that this is similar to data types bytes and short System.out.println(c); c = (char)(c+1); // Note that value of expression c+1 is within range of unicode // characters. But since its type is int which is bigger than that of // character, you have to do an EXPLICIT type cast. // Recall the similar rule in case of bytes and short. // Example : byte b = 23; byte b2 = b+1 will give compilation error System.out.println(c); int b = 2*c; char d = (char)(b/2); System.out.println(c); } }