r/learnjava • • Sep 22 '18

What exactly object is?

What happens internally when creating an object? What information is stored in ram about particular object? Object is nothing but a value in heap? I want to have deep insights about object.

0 Upvotes

7 comments sorted by

2

u/8igg7e5 Sep 22 '18

This isn't specified in the language (or the platform, though features of the stored object are required such as a monitor). Each JVM can do it differently, but they generally boil down to the following...

  • A pointer to the class (or it's vtable or an equivalent - but always traceable back to the class)

  • The object monitor, used in threads synchronising against this object.

  • Some flags such as GC state

  • Space for instance fields declared in the class of the object or a super-class

You should never need to care about the exact layout - especially as the optimiser could choose to elide unreferenced content or turn a 'new Object' into space on the stack instead of a heap allocation at all (via escape analysis).

0

u/NaveenCoder Sep 22 '18

Thanks. But i have natural tendency to learn everything deeply. Where i can learn about this more deeply?

2

u/8igg7e5 Sep 22 '18

You could look at the OpenJDK source-code (see the 'browse option') to see how their JVM manages memory. You'll need to know C as well as Java.

2

u/[deleted] Sep 22 '18

When an object is created, memory is allocated for that object.

Student student=new Student();

So here, we are creating an object of the Student class type, which has the name student.

Student student1=new Student();

Here, we are creating an object of the Student class type, with the name student1.

Memory will be allocated seperately for each object.

So, imagine memory is 100 for student. 101 is for student1 and so on.

0

u/NaveenCoder Sep 22 '18

What gets stored in memory address 100 and 101 ?

3

u/[deleted] Sep 22 '18

I recommend reading Processes and Process Control Block in Operating Systems.

1

u/GoodLifeWorkHard Sep 22 '18

IIRC, the data that makes up the class Student