Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

logical structure/details of a reference variable and object in the memory?

Let's say we have a class:

class Class1
{
   int i = 1;
}

and we have a variable:

Class1 ob1 = new Class1();
  • Does a reference itself stored in a variable ob1 store the information that it refers to an object of Class1?
  • Does the part of the heap where Class1 is stored store the information that it is of Class1 type?
  • How does logically looks like this information? It's a string like application1.Class1 or a reference to some reference types pool?

If you can recommend the source of such information I'll be very grateful for providing it I can't find it in the reference book.

like image 533
MaxNevermind Avatar asked Jun 26 '13 05:06

MaxNevermind


People also ask

Where are objects and reference variables stored?

All objects in Java are stored on the heap. The "variables" that hold references to them can be on the stack or they can be contained in other objects (then they are not really variables, but fields), which puts them on the heap also.

What is the difference between reference variable and object?

Reference Variables. A reference variable is a variable that points to an object of a given class, letting you access the value of an object. An object is a compound data structure that holds values that you can manipulate. A reference variable does not store its own values.

What is stored in a reference variable obj?

Answer and Explanation: The reference variable is used to store an object address or pointer of an object.


2 Answers

Does a reference itself stored in a variable ob1 store the information that it refers to an object of Class1?

NO. Reference variable ob1 stores only the reference of the object it points to. And the information about that object is already known to the application (or JVM).

Does the part of the heap where Class1 is stored store the information that it is of Class1 type?

NO. The information about class being loaded is stored in method area. As specified in this link
For each type it loads, a Java virtual machine must store the following kinds of information in the method area:

  • The fully qualified name of the type
  • The fully qualified name of the type's direct superclass (unless the type is an interface or class java.lang.Object, neither of which have a superclass)
  • Whether or not the type is a class or an interface
  • The type's modifiers ( some subset of` public, abstract, final)
  • An ordered list of the fully qualified names of any direct superinterfaces

How does logically looks like this information? It's a string like application1.Class1 or a reference to some reference types pool?

Inside the Java class file and Java virtual machine, type names are always stored as fully qualified names. For example, the fully qualified name of class Object in package java.lang is representated as java/lang/Object. In the method area, fully qualified names can be represented in whatever form and data structures a designer chooses.

like image 190
Vishal K Avatar answered Oct 22 '22 03:10

Vishal K


Every java object reference knows its class at runtime; this so-called "run-time type information" is used in code like this:

if (obj instanceof class1) {
  // true!
}

You can also access the class of an object through obj.getClass(). This will return class1.class, an object of class Class<class1>. See the Object.getClass method.

(Note that if your class is parameterized, as class1<T>, the type of T will not be stored at runtime, due to "erasure".)

I don't know whether the class information is stored with the pointer or with the data; it's probably implementation-specific in the jvm; but it hardly matters from a practical standpoint. (So either answer 1 or 2, or both, is "yes").

The answer to 3 is that, as far as a java programmer is concerned, the run-time type information is encapsulated in an object of class Class. Under the covers, a JVM may implement this in one way or another.

like image 40
Adam Bliss Avatar answered Oct 22 '22 02:10

Adam Bliss