Re: Is a byte data type really a 32-bit int in the JVM?
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.
I came across this tidbit saying within the JVM, a 'byte'
data type is actually a 32-bit integer:
http://www.jguru.com/faq/view.jsp?EID=13647
That's a terrible article; its author knows little or nothing about
Java. For instance , which it's true that
byte b = 0xAA;causes an error, all that's needed to remove it is
byte b = (byte)0xAA;
And
This is obviously of great consternation and gnashing of teeth to
C/C++
programmers who are used to reading in files as streams of
unsigned bytes,
or generating unsigned bytes, or saving space by using unsigned
bytes
is silly, since Java does the same things with its signed bytes.
In the Java Virtual Machine, bytes, shorts and ints are all four bytes
long.
is simply false.
etc.
Checking the JVM specification, I think I can confirm
that assertion. In sections 3.11.1 and 3.11.4, it is
mentioned that:
"As noted in ?3.11.1, values of type byte, char, and short
are internally widened to type int, making these conversions
implicit."
That means something else entirely, e.g. the fact that no cast is
required in
void methoda(int i)
{
byte b;
a(b); // "b" is implicitly converted to int for the
call
}