I don't understand the difference in output between System.out.println((char)b) and System.out.write((char)b)

Dennis Versluis :

I don't understand the difference in output between System.out.println((char)b) and System.out.write((char)b) in this example.

class WriteDemo {
    public static void main(String args[])
    throws IOException {
    byte data[] = new byte[10];
    int b;
    b = 'é';
    System.out.println((char)b);
    System.out.write((char)b);
    System.out.write('\n');
    }
}

System.out.println((char)b) prints é whereas System.out.write((char)b) prints Ú.

I don't understand why?

It probably has something to do with bits being discarded but I simply can't seem to figure out how it get to the letter Ú.

Nexevis :

write writes a byte to the output stream.

print writes a character to the output stream.

print also uses the platform's default encoding while write will write the byte as given according to the docs here.

Quote from the write docs:

to write a character that will be translated according to the platform's default character encoding, use the print(char) or println(char) methods.

Note: char is 16 bit, while byte is 8 bit, i.e. a char is two bytes thus losing information in the process of conversion.

EDIT:

The reason why write is outputting that character for you is because you are basically doing this:

System.out.write((byte) 233); //233 is Integer.valueOf('é')

Which is the same as this:

System.out.write(0xE9);

So in the encoding you are using where you print it 0xE9 is Ú.

Additionally, the reason why these end up printing as two different characters in the console is likely due to the char getting translated to the correct encoding for print and then printing in the same encoding as it was translated to, but because write writes the literal byte AND the console attempts to translate the byte into the current encoding anyway after the fact, it results in an incorrect character appearing. The character that appears will change depending on what the console encoding is set to, commonly Cp1252 or UTF-8.

If you change your console encoding to ISO-8859-1, then 0xE9 will print the é correctly and in both cases you included.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=134021&siteId=1