Content received from: http://JavaFAQ.nu/java-article63.html


Q: Why in the write method for the OutputStream class we have "int b"?
Friday, February 21, 2003 (00:00:00)

Posted by jalex

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? Smile



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.

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 255!

And last point: byte can not be used directly for mathematical operations, it has to be converted to int anyway.