Could anyone make me understand the term "object header" and "padding" in java?
class A{
int a;
}
what "object header" and "padding" would be here ?
Could anyone make me understand the term "object header" and "padding" in java?
class A{
int a;
}
what "object header" and "padding" would be here ?
 
    
    "Padding" is the tendency of objects in Java to be a multiple of 8 due to memory alignment: Why do Java objects have to be a multiple of 8?
All objects in Java contain an "object header" with a bit of extra overhead information (causing them to take up a little extra space): What is in java object header
So in your example, there would be space taken up by the header for class A along with the space needed to hold it's field int a. If these two values did not add up to a multiple of 8, extra space would be added to the memory location so that it was a multiple of 8, and therefore properly aligned in memory.
We can use an external library called jol to get into details of memory makeup while creating a class.
JOL : Java Object Layout, is a build tool that helps us in identifying the system properties, size of classes and objects in them. It tells the configuration of how a class is build and how much space is used by what component of the class.
This library can be easily found on mvn repository, just search jol-core.
As per your Question, the definition of 'A' Class is :
class A {
    int a;
}
Now, create another demo class and write this line in the main function:
public static void main (String[] args) {
    System.out.println(ClassLayout.parseClass(Temp.class).toPrintable());
}
This would print something like this which will clearly describe what is the memory mapping of a class in Java.
OFF  SZ   TYPE DESCRIPTION               VALUE  
 0   8        (object header: mark)     N/A     ->  The mark word describes the object header. 
                                                    The HotSpot JVM uses this word to store identity                                                                                                        
                                                    hashcode, biased locking pattern, locking information,
                                                    and GC metadata. 
 8   4        (object header: class)    N/A     ->  The class word encapsulates the language-level class
                                                    information such as class name, its modifiers,
                                                    superclass info, and so on. 
 12   4    int A.a                      N/A     ->  The class level variable 'a'.
 Instance size: 16 bytes
 Space losses: 0 bytes internal + 0 bytes external = 0 bytes total
Not Present in the current class but sometimes there is padding also in classes to make their size a multiple of 8. It is shown like this:
OFF  SZ    TYPE DESCRIPTOR              VALUE
25   3       (alignment/padding gap)    N/A  
Also, there is no value for the object header and the state because we're parsing a class layout, not an instance layout. Internally it uses the instrumentation class for this calculation.
Baeldung Article : Memory Layout of Objects in Java
