I'm still learning object-oriented programming in Java. I was looking at the Java implementation of java.io.Reader.skip
and I'm wondering why exactly it's implemented the way that it is. In particular I have questions about these things that I have noticed:
- The buffer used for the
skip(long)
is a field of the Reader object, rather than a normal variable in the method. - The maximum buffer length is much less than
Integer.MAX_VALUE
2147,483,647. In particular, Java's implementation uses 8192. java.io.InputStream
implements skip the same exact way.
Now, the reasons why I personally think that the buffer is a field, is so that the buffer won't have to be garbage collected repetitively due to being reinitialised repetitively. This might make skipping faster.
The buffer length being smaller I think has to do with making it so that the Reader blocks for shorter periods, but since the Reader is synchronized, would that really make a difference?
Byte streams implementing it the same way, might be for consistency. Are my assumptions correct on these three things?
To summarise, my questions are: About how much of a difference in speed on average does it make to use a field rather than a variable for character arrays? Wouldn't it be just the same to use Integer.MAX_VALUE
as the maximum buffer length? And isn't it better and easier to use the no-parameter read
method in a for-loop for byte streams since the other read
methods just call the no-parameter read
?
Sorry if my question's a strange question, but I think that I can learn a lot about object-oriented programming through this question.