crazy_rudy
crazy_rudy

Reputation: 533

Why does FileInputStream read method in java return a int, not a short?

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

Upvotes: 6

Views: 1596

Answers (5)

Ingo
Ingo

Reputation: 36339

The real reason is that the abstract class that FileInputStream inherits from is also the base for InputStreams that are supposed to read characters in any encoding, and it has a method

public int read();

that is used in character reading streams to read a single character.

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

Hence, a short could not be used, because it only has 32k (or 15 bits worth) positive values, but we need 64k (or 16 bits worth) for characters.

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

It is the same story with Readers of all kinds and sorts.

Upvotes: 1

Sachin Thapa
Sachin Thapa

Reputation: 3709

Read method returns int which signifies number of bytes read from the file, its a design decision taken while writing the API, now lets ask same question other way around:

How many maximum bytes we can read ?

•short: The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). You can use a short to save memory in large arrays, in situations where the memory savings actually matters.

•int: By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of -231 and a maximum value of 231-1.

Assuming we will have enough memory to hold bytes, int serves a better candidate over short and hence the API design. However API leaves the decision to user to decide how many bytes to read based on resources available.

Also, read method calls native code and on the native language side, these primitive data types are mapped to the closest matching native language data type.

Cheers !!

Upvotes: 1

Alfredo Osorio
Alfredo Osorio

Reputation: 11475

That's because read returns the number of bytes read, there is the following method defined in InputStream:

public int read(byte[] b) throws IOException

Reads some number of bytes from the input stream and stores them into the buffer array b. 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.

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

Upvotes: 1

oleksii
oleksii

Reputation: 35905

There are several reasons:

  • You can easily read more than 32 KB worth of data and with a short type this would cause an overflow
  • Performance of short equal to the performance of int due to the "bitness" of processors. Modern CPUs take numbers of 32 or 64-bits, which fits both int and short. There was a performance benefit when running code on old 16-bit processors. Long time ago...

Upvotes: 2

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726599

Java documentation on primitive types suggests that shorts should be used instead of ints to "save memory in large arrays":

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

Since in this situation memory savings do not actually matter, using int is a more consistent choice.

Upvotes: 8

Related Questions