Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose of byte type in Java

I read this line in the Java tutorial:

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.

I don't clearly understand the bold line. Can somebody explain it for me?

like image 898
Hải Phong Avatar asked Apr 24 '13 14:04

Hải Phong


People also ask

What is a byte data type?

The BYTE data type stores any kind of binary data in an undifferentiated byte stream. Binary data typically consists of digitized information, such as spreadsheets, program load modules, digitized voice patterns, and so on. The term simple large object refers to an instance of a TEXT or BYTE data type.

What is byte in Java code?

Byte Code can be defined as an intermediate code generated by the compiler after the compilation of source code(JAVA Program). This intermediate code makes Java a platform-independent language.

Why do we need data types in Java?

Data types are especially important in Java because it is a strongly typed language. This means that all operations are type-checked by the compiler for type compatibility. Illegal operations will not be compiled. Thus, strong type checking helps prevent errors and enhances reliability.


2 Answers

I imagine one can use byte for anything dealing with actual bytes.

Also, the parts (red, green and blue) of colors commonly have a range of 0-255 (although byte is technically -128 to 127, but that's the same amount of numbers).

There may also be other uses.

The general opposition I have to using byte (and probably why it isn't seen as often as it can be) is that there's lots of casting needed. For example, whenever you do arithmetic operations on a byte (except X=), it is automatically promoted to int (even byte+byte), so you have to cast it if you want to put it back into a byte.

A very elementary example:

FileInputStream::read returns a byte wrapped in an int (or -1). This can be cast to an byte to make it clearer. I'm not supporting this example as such (because I don't really (at this moment) see the point of doing the below), just saying something similar may make sense.

It could also have returned a byte in the first place (and possibly thrown an exception if end-of-file). This may have been even clearer, but the way it was done does make sense.

FileInputStream file = new FileInputStream("Somefile.txt");
int val;
while ((val = file.read()) != -1)
{
  byte b = (byte)val;
  // ...
}

If you don't know much about FileInputStream, you may not know what read returns, so you see an int and you may assume the valid range is the entire range of int (-2^31 to 2^31-1), or possibly the range of a char (0-65535) (not a bad assumption for file operations), but then you see the cast to byte and you give that a second thought.

If the return type were to have been byte, you would know the valid range from the start.

Another example:

One of Color's constructors could have been changed from 3 int's to 3 byte's instead, since their range is limited to 0-255.

like image 136
Bernhard Barker Avatar answered Oct 01 '22 03:10

Bernhard Barker


Byte has a (signed) range from -128 to 127, where as int has a (also signed) range of −2,147,483,648 to 2,147,483,647.

What it means is that since the values you're going to use will always be between that range, by using the byte type you're telling anyone reading your code this value will be at most between -128 to 127 always without having to document about it.

Still, proper documentation is always key and you should only use it in the case specified for readability purposes, not as a replacement for documentation.

like image 33
Mr. Adobo Avatar answered Oct 01 '22 01:10

Mr. Adobo