开发者

java byte data type

In Sun' tutorial it says about a byte:

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 limit开发者_Go百科ed can serve as a form of documentation.

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


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.

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;1 this 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.


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.

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.

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.

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.

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++)

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


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

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜