I was reading the byte stream trial and noticed the following statement
Notice that read() returns an int value. If the input is a stream of bytes, why doesn't read() return a byte value? Using a int as a return type allows read() to use -1 to indicate that it has reached the end of the stream.
The given reason for using an int
is that they can identify EOF by a -1. (seems shallow)
So the next bigger primitive type is short
and it also supports -1 so why not use it?
From what i gather: (reasons to use int
)
int
is preferred.
(this)
int
variable holds a character value in its last 16 bits (from
character
trial)Are my reasons correct? Am i missing something (like error correction)?
It returns an int because when the stream can no longer be read, it returns -1. If it returned a byte, then -1 could not be returned to indicate a lack of input because -1 is a valid byte.
read. Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255 . If no byte is available because the end of the stream has been reached, the value -1 is returned.
An attempt is made to read as many as len bytes, but a smaller number may be read. The number of bytes actually read is returned as an integer. This method blocks until input data is available, end of file is detected, or an exception is thrown.
InputStream is a source for reading data. A stream can represent various kinds of sources, including disk files, devices, other programs, and memory arrays. Streams support many different types of data, including simple bytes, primitive data types, localized characters, and objects.
read () : java.io.InputStream.read () reads next byte of data from the Input Stream. The value byte is returned in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned.
Applications that are defining subclass of InputStream must provide method, returning the next byte of input. A reset () method is invoked which re-positions the stream to the recently marked position.
When this read () method is called on the input stream then this read () method reads one character of the input stream at a time. It overrides read () method of FilterInputStream class.
If an I/O error occurs reading from the input stream, then it may do so after some, but not all, bytes have been read. Consequently the input stream may not be at end of stream and may be in an inconsistent state.
The most important reason to prefer int
over short
is that short
is kind of a second-class citizen: all integer literals, as well as all arithmetical operations, are int
-typed so you've got short
->int
promotion happening all over the place. Plus there is very little or no argument against the usage of int
.
There is only one scenario where using short
will give you an advantage: large arrays of short. To be sure, you can use them only when it is clear that the numbers to be stored fit the bounds.
In all other cases, it makes no real difference whether you have short
or int
. For example:
class A {
short s;
double d;
}
will not use less memory than:
class B {
int s;
double d;
}
because of alignment issues. So while the first one only has 10 bytes netto data, as compared to the second one that has 12, when you allocate an object it will still get aligned to some 8-byte boundary. Even if it is only a 4 byte boundary, the memory usage will be the same.
This is an interesting question :-) . It is true, that they had to use signed integer value type to represent EOF, but the preference of int
over short
is probably really just performance.
As I found on a different StackOverflow thread where this was discussed, the Java VM would automatically use int
internally even if the definition used short
.
The Java documentation states, that short should be used in large arrays and situations where memory really matters - source - http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html . That is apparently no the case here, because we always get just one value.
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