Re: Is a byte data type really a 32-bit int in the JVM?
Daniele Futtorovic wrote:
On 2008-02-03 21:38 +0100, Mike Schilling allegedly wrote:
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.
Yes. It seems there's been some confusion in this thread between the
byte type and the byte array (byte[]) type. They have little to do
with each other. The JVM doesn't create arrays of byte types, but
array-of-byte types, using the newarray op. How newarray is
implemented seems to be left up to the implementer, as far as I can
surmise, so the reasoning above would seem to be as much as one can
tell.
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.
The computational type for boolean, byte, short, char and int is
specified to be int, so this is more than "common implementation".
See:
<http://java.sun.com/docs/books/jvms/second_edition/html/Overview.doc.html#37906>
That says how computation is done on them, it doesn't prove that
they're not stored in their "natural" size and sign-extended to
perform ariuthmetic on them.
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.
boolean arrays and byte arrays share the same access ops: baload and
bastore. So, yes, an array of /n/ boolean will take up the same size
as an array of /n/ bytes. See:
<http://java.sun.com/docs/books/jvms/second_edition/html/Overview.doc.html#22909>
That's not proof either. If each array has a descriptor giving its
base type, the implementation of baload and bastore can access the two
types differently. See
http://java.sun.com/docs/books/jvms/second_edition/html/Instructions2.doc1.html:
"In Sun's implementation of the Java virtual machine, boolean arrays
(arrays of type T_BOOLEAN; see ?3.2 and the description of the
newarray instruction in this chapter) are implemented as arrays of
8-bit values. Other implementations may implement packed boolean
arrays; in such implementations the bastore instruction must be able
to store boolean values into packed boolean arrays as well as byte
values into byte arrays."