java java字节数据类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1987202/
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 18:49:33  来源:igfitidea点击:

java byte data type

javabyte

提问by mrblah

In Sun' tutorial it says about a byte:

在 Sun 的教程中,它说的是一个字节:

byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of documentation.

byte:字节数据类型是一个 8 位有符号二进制补码整数。它的最小值为 -128,最大值为 127(含)。字节数据类型可用于在大型数组中节省内存,其中内存节省实际上很重要。它们也可以代替 int 使用它们的限制有助于澄清您的代码;变量范围有限的事实可以作为一种文档形式。

How does it save memory? What is 2's compliment?

它是如何节省内存的?什么是2的赞美?

回答by Michael Petrotta

It saves memory by consuming only eight bits of storage, versus 32 for integers. The size of arrays is directly proportional to the size of the contained datatype; an array of integers will consume about four times more memory (handwaves) than an array of bytes.

它通过仅消耗 8 位存储空间来节省内存,而整数则为 32 位。数组的大小与包含的数据类型的大小成正比;整数数组将比字节数组消耗大约四倍的内存(handwaves)。

From Wikipedia:

来自维基百科

A two's-complement system or two's-complement arithmetic is a system in which negative numbers are represented by the two's complement of the absolute value;1this system is the most common method of representing signed integers on computers.[2] In such a system, a number is negated (converted from positive to negative or vice versa) by computing its two's complement. An N-bit two's-complement numeral system can represent every integer in the range ?2^(N?1) to +2^(N?1)?1.

二进制补码系统或二进制补码算法是一种用绝对值的二进制补码表示负数的系统;1该系统是在计算机上表示有符号整数的最常用方法。[2] 在这样的系统中,通过计算其二进制补码来否定数字(从正数转换为负数,反之亦然)。N 位二进制补码数字系统可以表示 ?2^(N?1) 到 +2^(N?1)?1 范围内的每个整数。

回答by Chad Okere

The other thing is that, mostly for historical reasons, most data is broken up into 8-bit bytes. It could have been any number, but 8-bit computers were really popular when things were really first getting standardized, I guess.

另一件事是,主要是由于历史原因,大多数数据被分解为 8 位字节。它可以是任何数字,但我猜,当事情真正开始标准化时,8 位计算机真的很受欢迎。

So for example, text is often stored with one 8-bit byte per letter (in ASCII mode). Data files are often indexed using pointers to byte indexes. People talk about kilobytes, and megabytes, and they mean 1024*8 bits. or 220* 8 bits.

例如,文本通常以每个字母一个 8 位字节的方式存储(在 ASCII 模式下)。数据文件通常使用指向字节索引的指针进行索引。人们谈论千字节和兆字节,它们的意思是 1024*8 位。或 2 20* 8 位。

Bytes are kind of the universal unit of computing for a lot of purposes. If you want to edit a standard file read by other programs, you're most likely going to need to load it into a byte[] and manipulate individual bytes at some point.

字节是多种用途的通用计算单位。如果您想编辑其他程序读取的标准文件,您很可能需要将其加载到 byte[] 中并在某个时候操作单个字节。

If sun didn't include a byte datatype, writing java programs that worked with data or text from other programs would have been a huge pain. You would have to load integers, and do shift and and operations to isolate individual bits, and divide indexes by 4 all the time. Not fun.

如果 sun 不包含字节数据类型,那么编写处理来自其他程序的数据或文本的 Java 程序将是一个巨大的痛苦。您将不得不加载整数,并进行移位和操作以隔离各个位,并始终将索引除以 4。不好玩。

So bytes weren't really added to save memory, but for compatibility sake.

所以字节并不是为了节省内存而真正添加的,而是为了兼容性。

Because a byte can have one of 28= 256 possible values, Sun decided they should denote -128 through 127, rather then 0 through 255, because they didn't want to deal with having both signed and unsigned numbers (all of their datatypes are signed, and Java doesn't have the unsigned keyword like C/C++)

因为一个字节可以有 2 8= 256 个可能值之一,Sun 决定它们应该表示 -128 到 127,而不是 0 到 255,因为他们不想同时处理有符号数和无符号数(它们的所有数据类型)是有符号的,并且 Java 没有像 C/C++ 那样的 unsigned 关键字)

They used two's complement because it's the standard way of dealing with negative numbers.

他们使用二进制补码,因为它是处理负数的标准方法。

回答by DarenW

Bytes save memory due to being only one byte long, whereas most other data types commonly used are 4 or 8 bytes long.

由于字节只有 1 个字节长,因此字节可以节省内存,而大多数其他常用数据类型的长度为 4 或 8 个字节。

Twos complement is the almost universal way to encode signed numbers as binary. This encoding has the nice property that incrementing any value as if it were just binary, gives you the next integer value, even as the value passes through zero. The same CPU circuitry can compute signed or unsigned integers.

二进制补码是将有符号数编码为二进制的几乎通用的方法。这种编码有一个很好的特性,它可以像二进制一样递增任何值,为您提供下一个整数值,即使该值通过零也是如此。相同的 CPU 电路可以计算有符号或无符号整数。