Anirudha
Anirudha

Reputation: 32797

Representing a single byte

A single byte takes up four bytes of space inside the Java virtual machine(32 bit processor).

Yes,we can use an array of byte which would occupy only the amount of space it actually needs. But I want to use a single byte not an array of bytes.

So,is there any type in Java to represent an 8 bit datum.

Upvotes: 1

Views: 163

Answers (5)

Brian Attwell
Brian Attwell

Reputation: 9299

A single byte can be allocated more than a single byte of storage, for memory alignment reasons.

Do not worry about the target processor. An array of 10000 bytes will be stored in approximately 10000 bytes of space.

Upvotes: 4

user1596371
user1596371

Reputation:

If you use byte then Java will use the most efficient method to store it. Might be 8 bits, might be 64 bits, but whatever it is it's for a good reason. Don't fight the compiler, it knows better than you.

Upvotes: 1

C-Otto
C-Otto

Reputation: 5843

It's up to the implementation (JVM) how to deal with the internal types. I guess any JVM on an 8bit machine uses 1 byte for the type byte - on 32bit or 64bit machines this might not always be the case, as you noticed :)

Upvotes: 1

MTilsted
MTilsted

Reputation: 5545

A byte does represent an 8 bit datum. Why do you care how many bytes an implementation of a vm uses to store it?

Upvotes: 0

poitroae
poitroae

Reputation: 21367

is there any type in Java to represent an 8 bit datum.

Yes, it is called byte. How much a single byte actually needs only depends on the Java VM.

Upvotes: 1

Related Questions