Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

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?

like image 210
crazy_rudy Avatar asked Oct 18 '13 15:10

crazy_rudy


1 Answers

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.

like image 69
Sergey Kalinichenko Avatar answered Oct 06 '22 01:10

Sergey Kalinichenko