在 Java 中是否有人使用 short 或 byte?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1539793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 17:00:38  来源:igfitidea点击:

In Java does anyone use short or byte?

java

提问by non sequitor

Apart from using (byte[]) in streaming I don't really see byte and short used much. On the other hand I have seen long used where the actual value is |100| and byte would be more appropriate. Is this a consequence of the relative inexpensive nature of memory now or is this just minutia that developers needn't worry about?

除了在流媒体中使用 (byte[]) 之外,我并没有真正看到 byte 和 short 的使用量。另一方面,我看到长期使用的实际值为 |100| 和字节会更合适。这是现在内存相对廉价的结果还是开发人员不必担心的细节?

回答by Shawn Mclean

They are used when programming for embedded devices that are short on memory or disk space. Such as appliances and other electronic devices.

它们在为内存或磁盘空间不足的嵌入式设备编程时使用。如电器和其他电子设备。

Byte is also used in low level web programming, where you send requests to web servers using headers, etc.

字节也用于低级 Web 编程,您可以在其中使用标头等向 Web 服务器发送请求。

回答by Stephen C

The bytedatatype is frequently used when dealing with raw data from a file or network connection, though it is mostly used as byte[]. The shortand short[]types are often used in connection with GUIs and image processing (for pixel locations & image sizes), and in sound processing.

byte数据类型在处理来自文件或网络连接的原始数据时经常使用,尽管它主要用作byte[]. 的shortshort[]类型通常与GUI和图像处理(像素位置的图像尺寸),并在声音处理结合使用。

The primary reason for using byteor shortis one of clarity. The program code states uncategorically that only 8 or 16 bits are to be used, and when you accidentally use a larger type (without an appropriate typecast) you get a compilation error. (Admittedly, this could also be viewed as a nuisance when writing the code ... but once again the presence of the typecasts flags the fact that there is truncation happening to the reader.)

使用byte或 的主要原因short之一是清晰。程序代码明确指出只能使用 8 位或 16 位,并且当您不小心使用更大的类型(没有适当的类型转换)时,您会收到编译错误。(诚​​然,这也可能被视为编写代码时的麻烦……但类型转换的存在再次表明读者发生了截断的事实。)

You don't achieve any space saving by using byteor shortin simple variables instead of int, because most Java implementations align stack variables and object members on word boundaries. However, primitive array types are handled differently; i.e. elements of boolean, byte, charand shortarrays are byte aligned. But unless the arrays are large in size or large in number, they doesn't make any significant contribution to the app's overall memory usage.

使用byteshort代替 简单变量并不能节省任何空间int,因为大多数 Java 实现都在字边界上对齐堆栈变量和对象成员。但是,原始数组类型的处理方式不同;即booleanbytecharshort数组的元素是字节对齐的。但除非数组很大或数量很大,否则它们不会对应用程序的整体内存使用量做出任何重大贡献。

So I guess that the main reason that developers don't use byteor shortas much as you (a C developer?) might expect is that it really doesn't make much (or often any) difference. Java developers tend not to obsessover memory usage like old-school C developers did :-).

所以我想,开发人员不使用byte或没有short您(C 开发人员?)可能期望的那么多的主要原因是它确实没有太大(或通常没有)差异。Java 开发人员往往不会像老派的 C 开发人员那样痴迷于内存使用:-)。

回答by Peter Lawrey

In a 64-bit processor, the registers are all 64-bit so if your local variable is assigned to a register and is a boolean, byte, short, char, int, float, double or long it doesn't use memory and doesn't save any resources. Objects are 8-byte aligned so they always take up a multiple of 8-byte in memory. This means Boolean, Byte, Short, Character, Integer, Long , Float and Double, AtomicBoolean, AtomicInteger, AtomicLong, AtomicReference all use the same amount of memory.

在 64 位处理器中,寄存器都是 64 位的,所以如果你的局部变量被分配给一个寄存器并且是一个布尔值、字节、短型、字符型、整数、浮点型、双精度型或长型,它不使用内存并且不会不节省任何资源。对象是 8 字节对齐的,因此它们在内存中总是占用 8 字节的倍数。这意味着 Boolean、Byte、Short、Character、Integer、Long、Float 和 Double、AtomicBoolean、AtomicInteger、AtomicLong、AtomicReference 都使用相同数量的内存。

As has been noted, short types are used for arrays and reading/writing data formats. Even then short is not used very often IMHO.

如前所述,短类型用于数组和读/写数据格式。即便如此,恕我直言,也不经常使用short。

Its also worth noting that a GB cost about £80 in a server, so a MB is about 8 pence and a KB is about 0.008 pence. The difference between byte and long is about 0.00006 pence. Your time is worth more than that. esp if you ever have a bug which resulted from having a data type which was too small.

还值得注意的是,服务器中的 GB 成本约为 £80,因此 1 MB 约为 8 便士,1 KB 约为 0.008 便士。byte 和 long 之间的差异约为 0.00006 便士。你的时间比这更有价值。尤其是如果你有一个由于数据类型太小而导致的错误。

回答by McDowell

I would most often use the shortand bytetypes when working with binary formats and DataInput/DataOutputinstances. If the spec says the next value is an 8bit or 16bit value and there's no value in promoting them to int(perhaps they're bit flags), they are an obvious choice.

在处理二进制格式和DataInput/ DataOutput实例时,我最常使用shortbyte类型。如果规范说下一个值是 8 位或 16 位值,并且没有将它们提升到(也许它们是位标志)的价值,那么它们是一个明显的选择。int

回答by Luke Woodward

Arithmetic on bytes and shorts is more awkward than with ints. For example, if b1and b2are two bytevariables, you can't write byte b3 = b1 + b2to add them. This is because Java never does arithmetic internally in anything smaller than an int, so the expression b1 + b2has type inteven though it is only adding two bytevalues. You'd have to write byte b3 = (byte) (b1 + b2)instead.

bytes 和shorts上的算术比在s上更笨拙int。例如,如果b1b2是两个byte变量,则不能编写byte b3 = b1 + b2将它们相加。这是因为 Java 永远不会在任何小于 an 的内部进行算术运算int,因此该表达式b1 + b2具有类型,int即使它只是将两个byte值相加。你必须改写byte b3 = (byte) (b1 + b2)

回答by Dinah

I used shortextensively when creating an emulator based on a 16-bit architecture. I considered using charso I could have stuff unsigned but the spirit of using a real integer type won out in the end.

short在创建基于 16 位架构的模拟器时广泛使用。我考虑过使用,char所以我可以有无符号的东西,但最终使用真正的整数类型的精神赢了。

edit:regarding the inevitable question about what I did when I needed the most significant bit: with the thing I was emulating it happened to almost never get used. In the few places it was used, I just used bitwise modifiers or math hackery.

编辑:关于当我需要最重要的一点时我所做的不可避免的问题:我正在模仿的东西几乎从未被使用过。在少数使用它的地方,我只使用了按位修饰符或数学技巧。

回答by Dean J

byte[] happens all the time; buffers, specifically for networks, files, graphics, serialization, etc.

byte[] 一直发生;缓冲区,专门用于网络、文件、图形、序列化等。

回答by Tom Hawtin - tackline

shortand others are often used for storing image data. Note that it is the number of bits which is really important, not the arithmetic properties (which just cause promotion to intor better.

short和其他的通常用于存储图像数据。请注意,真正重要的是位数,而不是算术属性(这只会导致提升int或更好。

shortis also used as array indexes in JavaCard (1.0 and 2.0, IIRC, but not 3.0 which also has an HTTP stack and web services).

short也用作 JavaCard 中的数组索引(1.0 和 2.0,IIRC,但不是 3.0,它也具有 HTTP 堆栈和 Web 服务)。

回答by C. Ross

I think in most applications short has no domain meaning, so it makes more sense to use Integer.

我认为在大多数应用程序中 short 没有领域意义,因此使用 Integer 更有意义。

回答by Charles Roberto Canato

Most of the time, there's never a real good technical reason for a developer (Java, C#, BASIC, etc.) to decide for an int, short or byte - when the capacity is enough, of course. If the value will be under 2 billion then int it will be.

大多数时候,对于开发人员(Java、C#、BASIC 等)来说,决定是 int、short 还是 byte 从来没有真正好的技术理由——当然,当容量足够时。如果该值将低于 20 亿,那么 int 将是。

Are you sure we'll have people older than 255? Well, you never know!

你确定我们会有 255 岁以上的人吗?好吧,你永远不知道!

Aren't 32,767 possible countries enough? Don't think too small!

32,767 个可能的国家还不够吗?别想得太小了!

In your example, you can be perfectly happy with your byte var containing 100, if you are absolutely sure than it will NEVER overflow. Why do guys use int the most? Because.... because.

在您的示例中,您可以对包含 100 的字节 var 感到非常满意,如果您绝对确定它永远不会溢出。为什么人们最常使用 int ?因为……因为。

This is one of those things that most of us just do because we saw it that way most of the time, and never asked differently.

这是我们大多数人所做的事情之一,因为我们大部分时间都是这样看的,而且从来没有提出不同的要求。

Of course, I have nothing against "all things int". I just prefer to use the right type for each kind of value, no stress involved.

当然,我不反对“all things int”。我只是更喜欢为每种值使用正确的类型,而不涉及压力。