Let's say we have a class:
class Class1
{
int i = 1;
}
and we have a variable:
Class1 ob1 = new Class1();
ob1
store the information that it refers to an object of Class1
? Class1
is stored store the information that it is of Class1
type? 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.
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.
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.
Answer and Explanation: The reference variable is used to store an object address or pointer of an object.
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:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With