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.
Chad okere
source share