What Java byte serves

Asked

Viewed 8,309 times

3

What is the byte type of java for? ex:

byte[] dados = ... ;

What’s the real use? Does anyone have an example?

3 answers

4


The byte is a type of database (primitive), which is the basis of programming, is the lowest level data type of Java, with it possible to create any other type.

1(a) byte is composed of 8(eight) bits;

In high-level languages such as Java, it is not as widely used in some cases. Where there are other derivatives types that are better suited to the case, such as String.

But if you program something that involves files for example, you will already come across a good utility for the same.

I’ll try to give you some examples:

  1. Read disk file, or download via network (HTTP or FTP);
  2. Write file on disk;
  3. Traffic raw data over the network;
  4. Image manipulation;
  5. Traffic of Streams;

These are some examples I’ve used byte, there is much more where one uses the type byte.

To documentation Java says the following about 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.

1

Well, byte is a data type consisting of 8 Bites. There are several uses of this type of data, among them, for example:

  • Masks;
  • Images, Strings, Dll’s, among many other data types/compiled objects;
  • Streams;
  • etc..

For example, in the case of streams, each byte received can be a character and at the end of the reception will have its message.

  • 2

    A byte is not a character, except in the simplest cases (ASCII). It is a mistake to match these two (in the same way as saying that a Unicode character is two bytes is also incorrect). I suggest reading of that Article (the translation is a little rough, but you can get the general idea).

1

Several utilities, are at most 255 bits can also as you posted can be an array of bytes. It is widely used when you want to save an entire file within a database. (Byte array). Depending on the proportion that the attribute of your class will require, using byte makes your application relatively lighter, as it is a type of primitive data.

  • 5

    "are at most 255 bits" I think you meant that it can represent values of 0 to 255, no? (a byte has 8 bits, no more, no less) Anyway, in Java there is no type unsigned byte (byte without signal), so that the representable values are -128 to 127. When one wants to represent values of 0 to 255, usually use a int even.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.