-6

What would be the size of the class object?

public class Example {

    int x = 10;
    String name = "Anuj Kumar";

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("hello java");
    }

}

Your help would be highly appreciated.

in th given class we have 2 data members so what will be the size of the class object, now when i changed it let say from int to float then what will be the size ? Thanks...

Anuj Kumar
  • 163
  • 10

1 Answers1

1

This is an excellent question , I assume when you say size of the class you meant the size of the object of the class In a 32 bit os Integer object takes 128 bytes 32 bit class pointer to class type 32 bit flags 32 bits locks 32 bits data ie int

String is basically an array of char So we have 2 objects java.lang.Sting and char[]

So if u look inside the String class we have Hash cont offset and value ie pointer to char array

So again 32 bit class pointer to class type 32 bit flags 32 bits locks 32 bits Hash 32 bits count 32 bits offset 32 bit pointer to char array 124 bits

char[] again has size and characters So 32 bit class pointer to class type 32 bit flags 32 bits locks 32 bits size 10 bits for actual char in ur case Anuj Kumar 138 bits

Again we have the Example class 32 bit class pointer to class type 32 bit flags 32 bits locks 32 bits for String pointer 32 bits for Integer wrapper 160 bits

Hence total size should be somewhere around 160+138+124 bits = 422 bits

Please take a look at this talk https://www.youtube.com/watch?v=FLcXf9pO27w which wonderfully expains the same

Not sure why people have downvoted the question instead of answering it

Ashish Daga
  • 46
  • 1
  • 3