Why is the reading method of FileInputStream in java returning int rather than short?

I know that a byte is not the proper type to contain the result of a read.
Thus, the read method returns a value of type int.
But I think the short type is more efficient than int.
It may contain a range value of -256 ~ 255.
Why does the read method return int rather than short?

+6
source share
5 answers

The Java documentation for primitive types suggests that short should be used instead of int to "save memory in large arrays":

short : The data type short is a 16-bit two-digit integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with byte , the same recommendations apply: you can use short to save memory in large arrays, in situations where it is actually important to save memory.

Since saving memory doesn't actually matter in this situation, using int is a more consistent choice.

+8
source

There are several reasons:

  • You can easily read data larger than 32 KB and with a short type, this will lead to overflow
  • The performance of short is equal to the performance of int due to the "bitness" of the processors. Modern processors occupy 32 or 64 bits, which corresponds to both int and short . Running code on older 16-bit processors had a performance advantage. A long time ago...
+2
source

That, since read returns the number of bytes read, the following method exists in an InputStream:

 public int read(byte[] b) throws IOException 

Reads a certain number of bytes from the input stream and stores them in buffer array b. The number of bytes actually read is returned as an integer . This method is blocked until the input data is available, the end of the file is detected or an exception is thrown.

The maximum length of the array is Integer.MAX - 5, since you can perform operations with arrays, then the return type is int.

+1
source

The read method returns int , which means the number of bytes read from the file, its design decision made when writing the API now allows you to ask the same question in another way:

How many maximum bytes can we read?

• short: the short data type is a 16-bit signed two padding integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). You can use shorthand to save memory in large arrays, in situations where saving memory is really important.

• int: By default, the int data type is a 32-bit signature of two integers that have a minimum value of -231 and a maximum value of 231-1.

Assuming we have enough memory to hold bytes, int is better for a shorter and therefore API design. However, the API leaves the decision for the user to decide how many bytes to read based on available resources.

In addition, the read method calls its own code on the side of the native language, these primitive data types are mapped to the nearest corresponding data type in the native language.

Greetings !!

+1
source

The real reason is that the abstract class inherited by FileInputStream is also the base for InputStreams, which must read characters in any encoding and have a method

 public int read(); 

which is used in character read streams to read a single character.

As we all know, char is an unsigned 16-bit integer type.

Therefore, the short one cannot be used, since it has only 32k (or 15 bits) positive values, but for characters we need 64k (or 16 bits).

And we need to (-1) pass the end of the file.

This is the same story with Reader all kinds and kinds.

+1
source

Source: https://habr.com/ru/post/956257/


All Articles