Re: Is a byte data type really a 32-bit int in the JVM?
Robert Dodier wrote:
Lew wrote:
Digital Puer wrote:
Is a byte data type really a 32-bit int in the JVM? More
specifically, if I have an an array of N byte types, are N
32-bit ints actually allocated underneath? I am writing
a memory-sensitive application and would appreciate
some insight.
From our point of view as Java programmers, we don't care.
Speak for yourself. Maybe you don't care, but the OP does care,
with good reason. Your sneering tone notwithstanding, you've
completely missed the point.
The real answer is "That's up to the JVM implementer; within the
language, there's no way to tell." Note that is a very different
answer than you'd get for C or C++, where there are lots of ways to
tell, sizeof(char) being the simplest one.
JVM implementers not being idiots, and arrays of bytes being used all
over the place in the system classes, I very much doubt that any JVM
implementation makes them four times as big as they have to be. It is
(IIRC) a common implementation that bytes used as local variables take
up a full 32 bits, but in normal cases that's a small enough fraction
of the total size of a new stack frame to be down in the noise.
A more interesting question is whether each entry in an array of
boolean takes up a full byte, when in principle only a single bit is
needed. An implementer needs to weigh the added cost of accessing a
single bit against the savings in space. I don't know what the usual
result is.