0

Related to char value assignment to an integer.

e.g. int x = 'A'; This statement will tend to assign 65 to i. But why ? As java uses UNICODE character encryption method, so it should store its Unicode equivalent to x which is '\u0041'. But why 65 ?????

11th Sep 2017, 6:52 AM
Divya Vishwakarma
Divya Vishwakarma - avatar
4 Réponses
+ 6
I suppose the '\uxxxx' is the hexadecimal representation of the number (65 in hex = 41) and as you can see from @ChaoticDawg answer, a char requires 2 bytes which also explains why there are 4 characters following the '\u' because they represent 2 hexadecimal values, which, combined together forms the 16 bit Unicode number. Hth, cmiiw
11th Sep 2017, 7:33 AM
Ipang
+ 5
@ChaoticDawg but I believe your answer should've got the mark, I was only adding a point to clarify. Thanks a lot @ChaoticDawg :)
11th Sep 2017, 8:08 AM
Ipang
+ 4
char values are based off of ascii character values. UTF-8 http://www.asciitable.com At least up to a certain point. The size of a char in java is 2 Bytes. This means that the max size of a Unicode charter it can hold is UTF-16 which is 2 Bytes. This is in fact was is actually held by a char. Smaller char characters that can be held by a single byte UTF-8 are pulled from the UTF-8 ascii table (0-255), while larger 2 byte characters are UTF-16. The UTF-8 characters that are used are still 2 bytes in size in memory, but are zero left padded by 8 bits (1 Byte). So why are the smaller UTF-8 values used when they can be? It apparently has to do with I/O streams in java and the fact that they are read by the byte. So characters that can be represented utilizing a single byte can read/write from the stream using only one byte instead of two. I'm guessing that this may also improve run time performance, since it would seem that a 1 byte character could be read in roughly half the time it would take for a 2 byte character. BTW '\u0041' this is still valid to assign to a char type and will result in 'A' char a = '\u0041'; System.out.println(a); outputs A More info: https://www.safaribooksonline.com/library/view/java-io/1565924851/ch01s03.html
11th Sep 2017, 7:32 AM
ChaoticDawg
ChaoticDawg - avatar
+ 4
@Ipang that's correct
11th Sep 2017, 8:03 AM
ChaoticDawg
ChaoticDawg - avatar