When Your Java Code is Running.
this post maybe be will be a separated post
Preface
Java is a programming language that is quite used today, according to a survey conducted by the stackoverflow team.[1] Java is still ranked as the most popular programming language.
How Java Works
In general, the java programming language is written in a .java source code file and then compiled with the java compiler and produces a .class file that can be run on jvm.
What Actually Compiler Do
In fact the compiler convert a source to a java class where the java class contains java bytecode. Java bytecode is the instruction set for the Java Virtual Machine. It acts similar to an assembler which is an alias representation of a C ++ code. As soon as a java program is compiled, java bytecode is generated. In more apt terms, java bytecode is the machine code in the form of a. Class file. With the help of java bytecode we achieve platform independence in java. [2]
Lets Try with Simple Java Program
public class Example1{
public static void main(String args[]){
System.out.println(“Hello World”);
cetak(“Awuu!!!!!”);
} public static void print(String message){
System.out.print(“Your message “ + message);
}
}// This is simple right?
Lets Generate The Bytecode
To see the bytecode generated by the compiler use the javap -c command or javap -v to verbose.
Constant pool: #1 = Methodref #2.#3
// java/lang/Object."<init>":()V #7 = Fieldref #8.#9
// java/lang/System.out:Ljava/io/PrintStream;
#13 = String #14
// Hello World
#15 = Methodref #16.#17
// java/io/PrintStream.println:(Ljava/lang/String;)V #21 = String #22
// Awuu!!!!! #23 = Methodref #24.#25
// Example1.cetak:(Ljava/lang/String;)V #28 = InvokeDynamic #0:#29
// #0:makeConcatWithConstants:(Ljava/lang/String;)Ljava/lang/String; #32 = Methodref #16.#33
// java/io/PrintStream.print:(Ljava/lang/String;)VCompiled from "Example1.java"
public class Example1 {
public Example1();
Code:
0: aload_0
1: invokespecial #1
4: returnpublic static void main(java.lang.String[]);
Code:
0: getstatic #7
3: ldc #13
5: invokevirtual #15
8: ldc #21
10: invokestatic #23
13: returnpublic static void print(java.lang.String);
Code:
0: getstatic #7
3: aload_0
4: invokedynamic #28, 0
9: invokevirtual #32
12: return
}
Lets Read the Bytecode
If you don’t know every function from every instruction, maybe this will help you [3]. Let me explain a little, in general, the java language has a default constructor [4] because in the example we did not make the constructor explicitly then java will initiate the default constructor. This is indicated by the following line.
public class Example1 {
public Example1();
Code:
0: aload_0
1: invokespecial #1
4: return
If you use verbose you can see the line descriptor and flag, which explains the access modifier and the parameters of the constructor. So it look like the following line. If the parameter of constructor is integer so it will “descriptor: (I)V”. You can check in [5] actually.
public class Example1 {
public Example1();
descriptor: ()V
flags: (0x0001) ACC_PUBLIC
Code:
0: aload_0
1: invokespecial #1
4: return
Temporary Summary
Understand java compiler actually helping you to understand how java actually works, and how java source code can be run one Java Virtual Machine. Its a simple things but cool topic to understand. Its just only temporary cause any related subject cannot included just only post. In another post i will try to figure out how Java Virtual Machine work, how Java Just In Time can be efficient the java proccess, how to see the stack on JVM when debugging java and how to run Java in another Virtual Machine its mean not JVM DUDE!!!.
Reference
[1] https://insights.stackoverflow.com/survey/2019#top-paying-technologies
[2] https://www.javatpoint.com/java-bytecode
[3] https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-6.html
[4] https://www.geeksforgeeks.org/g-fact-50/
[5] https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html