1

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?

7
  • 1
    Well one byte are 8 bits, which represent the binary data Commented Aug 22, 2018 at 10:07
  • 1
    Doesn't your hard drive is a Tera-Byte array ;) Commented Aug 22, 2018 at 10:07
  • 1
    Java, for several reasons including legacy reason, chose to make byte the smallest primitive type which can be used to represent binary data (well boolean conceptually represents a bit, but the Java API doesn't use it for binary data). Commented Aug 22, 2018 at 10:15
  • 1
    @TimBiegeleisen: So to conclude 1 Byte == 8 bits , and Byte array == stream of bits, and hence represent binary data? Commented Aug 22, 2018 at 10:21
  • 2
    That's my understanding of it...let someone else shoot me down if I'm off my rocker. Commented Aug 22, 2018 at 10:21

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.