Java byte data type

The Sun tutorial says about byte:

byte: The byte data type is an 8-bit two-digit 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 memory saving is really important. They can also be used instead of int, where their limitations help clarify your code; the fact that the range of variables is limited can serve as a form of documentation.

How does he save memory? What is 2 compliments?

+6
java byte
source share
3 answers

Saves memory by consuming only eight bits of memory, versus 32 for integers. The size of the arrays is directly proportional to the size of the data type it contains; an array of integers will consume about four times as much memory (manual) as an array of bytes.

From Wikipedia :

A system with two additions or arithmetic with two additions is a system in which negative numbers represented by two additions are an absolute value; 1 this system is the most common method of representing integers on computers. [2] In such a system, a number is negated (converted from positive to negative or vice versa) by calculating two complements. An N-bit-two-complement numeric system can represent each integer ranging from -2 ^ (N-1) to + 2 ^ (N-1) -1.

+10
source share

Another thing is that mainly for historical reasons, most of the data is divided into 8-bit bytes. It could be any number, but 8-bit computers were really popular when everything was really standardized, I think.

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

Bytes are a universal unit of computation for many purposes. If you want to edit a standard file read by other programs, you will most likely need to load it into byte [] and manipulate individual bytes at some point.

If the sun did not include a byte data type, writing java programs that worked with data or text from other programs would be a huge pain. You will need to load integers, as well as perform shift and operations to extract individual bits and divide the indices by 4 all the time. Not fun.

So bytes were not actually added to save memory, but for compatibility.

Since a byte can have one of 2 possible values, Sun decided that they should indicate -128 to 127, and not from 0 to 255, because they did not want to deal with both signed and unsigned numbers (all of their data types signed but Java doesn't have unsigned keyword like C / C ++)

They used two additions because this is the standard way to solve negative numbers.

+2
source share

Bytes retain memory due to the duration of only one byte, while most other commonly used data types are 4 or 8 bytes.

The Twos component is an almost universal way to encode signed numbers as binary. This encoding has a nice property, which increments any value, as if it were just binary, giving you the next integer value, even when the value goes through zero. The same CPU circuitry can calculate signed or unsigned integers.

0
source share

All Articles