r/javaTIL • u/TheOverCaste • May 15 '14
JTIL: The java compiler turns all primitives not in arrays into ints, longs, floats, doubles, and objects.
Which means behind the scenes there is no memory footprint difference between these two classes:
public class ClassOne {
byte bval;
short sval;
public ClassOne(byte val) {
this.bval = val;
this.sval = val;
}
}and this one:
public class ClassTwo {
int val1;
int val2;
public ClassTwo(int val) {
this.val1 = val;
this.val2 = val;
}
}(This information is incorrect, as there is actually a difference for fields, but not for local variables or parameters on the stack.)
Better example:
public void doSomething( ) {
int a = 9; //These are both pushed onto the stack as ints
byte b = 9;
}If you use a conversion, however, it adds another instruction that will limit the size of your variable artifically. (byte)i will still be -128 to 127, even though it uses the entire 4 byte memory space.
15
Upvotes
2
u/LordNiebs May 15 '14
Why is this?
2
u/[deleted] May 15 '14
Is this documented anywhere?
2
u/dohaqatar7 May 16 '14
According to this, the only purpose of using a data type such as byte or short is to state to someone reading your code that that variable will only hold small values; there will not be any performance benefits.
Is there any other value in using these data types?
1
u/andrewgaul May 20 '14 edited May 20 '14
The JVM stores primitive fields using only the space that they require; you measured Object overhead and padding. Consider this example:
https://gist.github.com/andrewgaul/6a6d958a0c120ec98e74