Is there a package that defines the size of Java primitives that I can import in my project? I'm doing some manual bit setting and I keep a byte index. I don't want to do currentByte += 4
when I set an int (magic numbers are frowned upon), I rather do currentByte += <SomePackage>.SIZE_OF_INT
I guess I can define my own, but that's kind of clunky, especially if those are available somewhere.
Not a class, but you have Integer.SIZE
, and so on for Long
and floating point classes too. You also have *.BYTES
.
Therefore Integer.SIZE
is 32, Integer.BYTES
is 4, Double.SIZE
is 64 and Double.BYTES
is 8, etc etc; all of these are int
s in case you were wondering.
NOTE: *.BYTES
are only defined since Java 8 (thanks @Slanec for noticing)
(*.SIZE
appeared in Java 5 but you do use at least that, right?)
And yes, this is defined by the JDK since the JLS itself defines the size of primitive types; you are therefore guaranteed that you'll have the same values for these constants on whatever Java implementation on whatever platform.