Q: Why in the write method for the OutputStream class we have "int b"?
JavaFAQ Home » Daily Tips
Question: Why in the write method for the OutputStream class we have "int b"? Was not it possible to have public abstract void write(byte b)?
I am confused: In javadoc for
public abstract class OutputStream:
"public abstract void write(int b) throws IOExceptionWrites the specified byte
to this output stream."
In the same description later: "The 24 high-order bits of b are ignored" ...?
Why do we need to use int and ignore 3/4 of that afterwards...
Has it relation to memory management efficiency on 32-bits CPUs and OSs?
What's about 64-bit CPUs like Itanium... Will SUN change int to 64-bits size?
Or does SUN believe that in future bytes will be 32 bits long?
Answer: Equivalent method read() returns an int also. This is to enable a return value of -1 in which is not possible if use
Byte is signed and it has the value range: -128 to 127. And even if the sign is
not so important there will not be any possibility to distinguish between -1 and
And last point: byte can not be used directly for mathematical operations, it
has to be converted to int anyway.
Printer Friendly Page
Send to a Friend
Search here again if you need more info!