How come this happens:
char a = '\uffff'; //Highest value that char can take - 65535 byte b = (byte)a; //Casting a 16-bit value into 8-bit data type...! Isn't data lost here? char c = (char)b; //Let's get the value back int d = (int)c; System.out.println(d); //65535... how?
Basically, I saw that a char
is 16-bit. Therefore, if you cast it into a byte
, how come no data is lost? (Value is the same after casting into an int)
Thanks in advance for answering this little ignorant question of mine. :P
EDIT: Woah, found out that my original output actually did as expected, but I just updated the code above. Basically, a character is cast into a byte and then cast back into a char, and its original, 2-byte value is retained. How does this happen?
Every character type in Java occupies 2 bytes in size. For converting a String to its byte array equivalent we convert every character of the String to its 2 byte representation.
A byte is 8 bit . char is 16 bit .
The char type takes 1 byte of memory (8 bits) and allows expressing in the binary notation 2^8=256 values. The char type can contain both positive and negative values. The range of values is from -128 to 127.
It is mainly because everything is generally converted to integers in Java. int really is the main type in Java; calculations of bytes, shorts and chars are all widened to integer types during such a calculation. This conversion is just a basic but weird example of this. This will convert a byte array to the default charset in java.
Answer: Yes, by using the charAt () method, you can easily convert String to Java char. Given below is an example of printing char values. Q #4) Why is char used in Java? Answer: Java char is a primitive data type that is used to declare the Java character variables.
The program reads an input string from the command line and stores it in a string, read the character at the specified position using the charAt () method and store it in a character type variable, convert this variable into byte by typecasting and then print the value at the console. Output this program.
If you want to encode/decode characters from bytes, use Charset, CharsetEncoder, CharsetDecoder or one of the convenience methods such as new String (byte [] bytes, Charset charset) or String#toBytes (Charset charset). You can get the character set (such as UTF-8 or Windows-1252) from StandardCharsets.
As trojanfoe states, your confusion on the results of your code is partly due to sign-extension. I'll try to add a more detailed explanation that may help with your confusion.
char a = '\uffff'; byte b = (byte)a; // b = 0xFF
As you noted, this DOES result in the loss of information. This is considered a narrowing conversion. Converting a char to a byte "simply discards all but the n lowest order bits".
The result is: 0xFFFF -> 0xFF
char c = (char)b; // c = 0xFFFF
Converting a byte to a char is considered a special conversion. It actually performs TWO conversions. First, the byte is SIGN-extended (the new high order bits are copied from the old sign bit) to an int (a normal widening conversion). Second, the int is converted to a char with a narrowing conversion.
The result is: 0xFF -> 0xFFFFFFFF -> 0xFFFF
int d = (int)c; // d = 0x0000FFFF
Converting a char to an int is considered a widening conversion. When a char type is widened to an integral type, it is ZERO-extended (the new high order bits are set to 0).
The result is: 0xFFFF -> 0x0000FFFF
. When printed, this will give you 65535.
The three links I provided are the official Java Language Specification details on primitive type conversions. I HIGHLY recommend you take a look. They are not terribly verbose (and in this case relatively straightforward). It details exactly what java will do behind the scenes with type conversions. This is a common area of misunderstanding for many developers. Post a comment if you are still confused with any step.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With