简体   繁体   English

为什么InputStream read()返回一个int而不是short?

[英]Why does InputStream read() return an int and not a short?

I was reading the byte stream trial and noticed the following statement 我正在阅读字节流试用,并注意到以下声明

Notice that read() returns an int value. 请注意,read()返回一个int值。 If the input is a stream of bytes, why doesn't read() return a byte value? 如果输入是字节流,为什么read()不返回字节值? Using a int as a return type allows read() to use -1 to indicate that it has reached the end of the stream. 使用int作为返回类型允许read()使用-1来表示它已到达流的末尾。

The given reason for using an int is that they can identify EOF by a -1. 使用int原因是它们可以通过-1识别EOF。 (seems shallow) (似乎很浅)

So the next bigger primitive type is short and it also supports -1 so why not use it? 所以下一个更大的原始类型是short ,它也支持-1所以为什么不使用它?

From what i gather: (reasons to use int ) 从我收集的内容:(使用int原因)

  1. Due to performance int is preferred. 由于性能int是首选。 (this) (这个)
  2. int variable holds a character value in its last 16 bits (from character trial ) int变量在最后16位中保存一个字符值(来自字符试验
  3. Other more abstract streams would need to read more than just one byte (something that i guess (happens with character streams)) 其他更抽象的流需要读取的不仅仅是一个字节(我猜的是(在字符流中发生))

Are my reasons correct? 我的理由是否正确? Am i missing something (like error correction)? 我错过了什么(比如纠错)?

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. 更喜欢int over short的最重要的原因是short是一种二等公民:所有整数文字,以及所有算术运算,都是int -typed所以你有short - > int促销发生在整个地点。 Plus there is very little or no argument against the usage of int . 另外, int的使用很少或根本没有争论。

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. 确实,他们必须使用有符号整数值类型来表示EOF,但intshort的偏好可能只是性能。

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 . 正如我在另一个讨论过的StackOverflow线程中发现的那样,即使定义使用short ,Java VM也会在内部自动使用int

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 . Java文档指出,short应该用在大型数组和内存真正重要的情况下 - 源代码 - http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html That is apparently no the case here, because we always get just one value. 这显然不是这种情况,因为我们总是得到一个值。

There is only one scenario where using short will give you an advantage: large arrays of short. 只有一种情况是使用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 . 在所有其他情况下,无论是short还是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. 因此,第一个只有10个字节的netto数据,而第二个有12个,当你分配一个对象时,它仍然会对齐到一些8字节的边界。 Even if it is only a 4 byte boundary, the memory usage will be the same. 即使它只是一个4字节的边界,内存使用也是一样的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM