I've read in a couple of my books that you can cast a char to an int, but I've seen no examples and I'm having a tough time understanding this concept. Would it be like this? (int)c And then how is c represented by an integer value? I mean, c is a letter, not a number. I'm confused here Thanks!
hi, It is not necessary that you have to do an explicit cast in this case. As character is 16 bits and integer is 32 bits, the integer can hold the value of 16 bits. So there is no need of the statement (int)c; But if you want to store the value of int to a char then you need to do an explicit cast as c = (char)I; // if I is an integer and c is a character Also note that when converting a char to an int the value is always positive as char is unsigned.
I am still confused by this. Example: String strA = "A"; char charA = strA.charAt(0); int intA = charA; When printing the value of intA you get 65 which is the ASCII decimal value of "A". Why isn't the value of intA a Unicode value?