

i in iconst_1 means integer type, const – constant, 1 – a value of the constant. When JVM encounters iconst_1 instruction it pushes 1 on the operand stack. Let’s have a look how JVM executes bytecodes operations with a simple example of the sum of two numbers. It serves for linking other parts of the runtime image with a current method. A class constant pool is a kind of storage of different data, such as names of the other classes and methods, variable types and so on. Thus, JVM has the exact information on how much memory it needs to allocate on a thread stack for a particular method.
#Openjdk 7 jvm internals code
It can be seen in Code: section as stack=0 in the code snippet above. The size of the operand stack is also precomputed. JVM either executes bytecode instruction that pushes an operand on the stack or pops needed operands number for an instruction and executes the instruction. JVM uses the operand stack to execute bytecode instruction. The local variable array contains values of local variables for primitives and references to objects on the heap. You should have seen this in Code: section with locals=5 in the previous section example. The size of the array is known during execution time, and is precomputed before the runtime phase. JVM allocates memory for local variables array and puts all method arguments at the beginning of the array. A local variables array is used to store method’s local values and method’s argument values. JVM method frame contains local variables array, operand stack and reference to a current class constant pool. Every time JVM invokes a method it creates a frame for the method on a current thread stack. Let’s get started with methods structure at runtime. Then, we’ll take a closer look at a dynamic languages example and what developers can do to overcome constraints that JVM enforces. Let’s take a look at how JVM deals with method invocation and its execution. Static languages need to have a smart compiler that will produce valid bytecode, whereas, dynamic languages perform a lot of tricks to satisfy JVM requirements of a method bytecode. What I want to focus on right now is that JVM requires that all methods have a descriptor with classes/types – thus static languages are in a better situation than dynamic ones. We will take a look at methods structure in the next sections of this article. some bytecode is omittedĪny method has to have a descriptor, access flags, and code. Stack = 0, locals = 5, args_size = 4 0 : return //. Object ) ĭescriptor : (ILjava /lang / String JLjava /lang / Object )Vįlags : (0x000a ) ACC_PRIVATE, ACC_STATIC some bytecode is omitted private static void someMethod ( int, java. We’ll compile it with javac and read with javap program to see the bytecode representation of the method: Let’s take a look at a Java method with a few arguments of different types, and see what bytecode javac generates to execute it on JVM: Methods are the main artifacts of a programming language. Any language that runs on JVM should have a compiler or a runtime that produces a valid bytecode that JVM understands and executes. Java bytecode is an interface between a programming language and JVM as an execution machinery. Generally speaking, all programs that are written in the wild are a bunch of methods structured in some manner to solve a particular problem. In this article, I will take a look at how JVM handles method invocation and its support for static and dynamic programming languages. For this reason, JVM is a great execution engine for languages other than Java, such as Scala and Kotlin, which are statically typed, and Groovy and Ruby, which are dynamically typed languages. Despite the fact that Java language and JVMs are influencing each other’s designs, engineers that work on JVMs are trying to make them as generic as possible.

Initially, JVM was designed at Sun Microsystems to execute Java programs.

JVM has a number of implementations, the most popular being HotSpot JVM, which will be used as an example of the implementation throughout the article. Java Virtual Machine (JVM) is an abstract virtual machine that executes Java bytecode.
