CuriousMind
CuriousMind

Reputation: 8943

How byte array in Java is used to represent a binary data?

I have read that byte array in Java is used to represent a binary data. I am not able to understand this. How byte array can represent a binary data (and which can be transferred over the network and can be constructed back to original form).

Byte can have (integer) values from -128 to 127; so how does a byte array represent a binary data?

Upvotes: 1

Views: 1479

Answers (1)

Niko Kiirala
Niko Kiirala

Reputation: 194

Byte can be (integer) values -128 to 127, so how does a byte array represent a binary data?

Each byte (octet) is a sequence of eight bits, and having sequence of bytes lets us represent binary data of any length (though it's limited to per 8-bits increments).

Memory of most modern computers is addressed as a sequence of bytes, network interfaces send packets containing sequences of bytes, hard drives store sequences of bytes (but are addressable only in much larger blocks, say, 4096 bytes).

There is rarely need to access data bit-by-bit, and when needed it can be done with bitwise operators, so no data type for sequence of bits is provided by default.

So to conclude:
1 Byte == 8 bits, and Byte Array == stream of bits, and hence represent binary data?

Yes. For example: A Byte Array of length 2 bytes is a stream of 16 bits of binary data.

Upvotes: 2

Related Questions