primitive sizes in bytes

ok, I found an article on bytebuffers and nio on the net and it stated the following:

a short is 2 bytes in size
an int is 4 bytes in size
a long is 8 bytes in size

If these are true what size is a float? and are these accurate for the java lang?

thanks

Accurate. I believe that…

Float is 4.
Double is 8.

Could be wrong of course.

Kev

thanks! I will give it a whirl!

Yep, those values are right.

You can read the below pages for a whole list w/ min and max values as well.

One thing to keep in mind is that all Java primitives (except boolean & character) are signed.
This can be annoying/a source of errors when reading or writing data from languages that allow unsigned values.

As an example, many file formats will use one byte to store values between 0 and 255. If you tried to read this into a Java byte, anything above 127 would be read in as a negative number, because Java bytes only go from -128 -> 127; you don’t have the option of making it unsigned.
Hence, you have to read such values into the next type up, i.e. 1-byte unsigned values must be read into a short, 2-byte values into an int, etc.

Links:

http://www.chinalinuxpub.com/doc/oreillybookself/java/javanut/ch02_06.htm

OR

http://www.scism.sbu.ac.uk/jfl/Appa/appa1.html
http://www.scism.sbu.ac.uk/jfl/Appa/appa2.html

Something to remember that might be obvious…

These are the sizes of their representations in a ByteBuffer.

What size a primative variable is in memory is totally VM dependnat.

(For instance I believe its still the case that the Sun VM internally stores indvidual byte variables in a full int.)

[quote]What size a primative variable is in memory is totally VM dependnat.

(For instance I believe its still the case that the Sun VM internally stores indvidual byte variables in a full int.)
[/quote]
I’ve heard this before to and it stands to reason.
It seems likely that VMs written for a 32-bit system would use 32-bit words for everything that’s either 32 bits (int) or smaller.

You’d probably find on phones (which I assume have 8- or 16-bit architectures?) this isn’t the case.