0

I am trying to understand how the class file is loaded into method area and execute. I am very much confused about the constant pool.

  1. when the constant pool is created initially? while compiling the class file or when the class is loaded.

  2. How the byte code is organized in method area What the method table consists of?

  3. Can anyone show the sketch the picture representation of mapping in method area for clear understanding
Guest
  • 21
  • 6

3 Answers3

1

Since the literal meaning of “constant pool” is just “pool of constants”, there are different things of the name, which are easy to confuse

  1. Each class file has a constant pool describing all constants used in that class, which includes constant values but also symbolic references needed for linkage. Some entries fulfill both roles, e.g. class entries may serve as owner declaration for a symbolic reference to a member, needed when accessing a field or invoking a method, but may also be used to get a Class instance, e.g. for a class literal appearing in source code. Since it’s part of the class file, its format is specified within The Java® Virtual Machine Specification, §4 The class File Format, in §4.4. The Constant Pool.
    As said by other answers, you can use the command javap -v class.name to inspect the constant pool of a class.

  2. There is a corresponding data structure at runtime, also known as run-time constant pool. Since certain values are represented as runtime objects (e.g. of type String, Class, MethodType, or MethodHandle), and symbolic references must be resolved to the runtime representation of the denoted classes and members, this structure is not the same as the byte sequence found in the class file. But these entries correspond, so that each time, an object is instantiated for a constant or a symbolic reference is resolved, the result can be remembered and reused the next time the same constant entry is accessed.

    This doesn’t imply that an implementation must have a 1:1 representation of each class’ constant pool. It’s possible that a specific implementation maps a class’ pool to a shared pool used for a all classes of the same class loading context, where each symbolic reference resolves to the same target.

  3. There’s also the string pool, which can be seen as part of the runtime constant pool, holding references to all String instances associated with string constants, to allow resolving all identical string constants of all classes to the same String instance.

Community
  • 1
  • 1
Holger
  • 243,335
  • 30
  • 362
  • 661
  • Is constant pool created while compilation time and runtime constant pool both are same or it will compilation constant pool is copied into runtime constant pool? – Guest Dec 26 '17 at 12:54
  • As said, they correspond, but they are not identical, as the runtime representation is not identical to the bytecode format, e.g. numbers in bytecode are always `big endian`, x68 based systems use `little endian` at runtime, strings and symbols are encoded in bytecode using `modified UTF-8`, at runtime, `UTF-16` and `ISO-LATIN-1` are used, also memory addresses to existing objects and data structures will be used at runtime. You may see it as a lazy copy and conversion operation will take place. – Holger Dec 27 '17 at 13:06
  • Thanks for explanation. One more doubt is run time constant pool is referred as string pool for String or String pool is created in separate place? – Guest Dec 28 '17 at 03:49
  • That’s implementation dependent, but usually, there are two different runtime data structures; the constant pool is organized as table whose indices correspond to the indices used in the class file, whereas the string pool is a hash table, corresponding to the actual string contents. But they refer to the same string instances for resolved string constants. – Holger Dec 28 '17 at 11:56
0

When a Java file is compiled, all references to variables and methods are stored in the class's constant pool as a symbolic reference.

Here is a link for your reference : What is the purpose of the Java Constant Pool?

Curious Techie
  • 185
  • 1
  • 13
0
  1. javac creates a constant pool when you compile your source to .class file. You can see it if you make

javap -v MyClass

to your MyClass.class

  1. The Java Virtual Machine has a method area that is shared among all Java Virtual Machine threads. You can see bytecode of your class file by

'javap -c -v Main'

  1. Method Area is just a part of the heap where JVM has all information about this class.