java - How are class attributes/fields stored -


i know instance of c++ class :

class {    char c;    int iiii;    short ss;    }; 

would kind of in memory : c| | | |i|i|i|i|s|s| | |

this 4/2 letter annotation has no sense (but think point clear)

1 byte char, 3 bytes of padding, 4 bytes of int, 2 bytes short, , 2 bytes of tail padding (platform dependant, won't change logic)

from c++ standards (compilers wouldn't change order of fields in example):

nonstatic data members of (non-union) class same access control (clause 11) allocated later members have higher addresses within class object. order of allocation of non-static data members different access control unspecified (clause 11). implementation alignment requirements might cause 2 adjacent members not allocated after each other; might requirements space managing virtual functions (10.3) , virtual base classes (10.1).

so, know if same java classes, can compiler change order reduce padding ?

first have @ what java objects in memory during run-time? , know thy java object memory layout .

but guess java objects memory structure answers question.

in order save memory, sun vm doesn't lay out object's attributes in same order declared. instead, attributes organized in memory in following order:

doubles , longs ints , floats shorts , chars booleans , bytes references 

Comments