|
|
|
|
|
Example that show you the same binary number is "interpreted" differently:
char c; byte b; c = 65; b = 65; System.out.println("char c = " + c); // Prints "A" System.out.println("byte b = " + b); // Prints "65" System.out.println(); System.out.println("How to change 'interpretation' of a binary number"); System.out.println("char c casted by (byte) = " + (byte)c); System.out.println("byte b casted by (char) = " + (char)b); |
Output:
char c = A byte b = 65 How to change 'interpretation' of binary number char c casted by (byte) = 65 byte b casted by (char) = A --- You can see that variables c and b contains the SAME binary number !!! --- By putting a casting operation, the number 65 is printed different (because a different println( ) function is called using different data type - through the overloading feature in Java |
How to run the program:
|
|
|
Example:
int x = 'C' - 'A'; // Subtract the ASCII codes as binary numbers.... // x = 2 (in binary) |
How to run the program:
|
byte b; char c; b = (byte) c; // Casts character value in c to byte type c = (char) b; // Casts byte value in b to char type |
Why do you want to do this:
|
How to run the program:
|
|
Answer:
I |
How to run the program:
|