Programming in Java: From writing to execution


In this article, I will explain different phases through which a java application passes, from a programmer idea to its execution in a real system. I’ve based on Deitel explanation, from his book “How to program in Java”

A java application has following stages:

  1. Edition or Creation Phase
  2. Compilation Phase
  3. Loading a program into memory Phase
  4. Bytecodes Verification Phase
  5. Execution Phase

Another texts just proposes three phases, which includes described phases above . These could be:

  1. Edition
  2. Compilation
  3. Interpretation or Execution

I will choose the first list to explain, because I consider that is more detailed.

Edition or Creation Phase

First thing a programmer do when he has an idea of what he want, is start to write the code. This is know as edition phase and its result is the source code. This is a kind of code understandable by people, similar to english, but is far to be understandable for a computer.

We can write a java applications using simple programs such as text editor from Windows; vi, vim or gedit in Linux, or we could use specialized software to do this known as IDE (Integrated Development Environment), these could be Eclipse, IntelliJ, Sublime, etc.

File which contains  the source code for a java application will have a .java extension. When we see in our projects .java files, we will know that these kind of files are source codes.

A simple application example which prints a text in a console is this:

public class SimpleApp{

public void main(String[] args){
System.out.println("This is a simple application");
}
}

The name of the .java file must match with the name of the class, in this case SimpleApp.java

Compilation Phase

Now, is time to compile the source code.

This process converts our source code in byte codes, which will be interpreted by JVM later.

Unlike other languages, compiled bytecodes can run in several systems (Linux, Windows, MacOS, etc), is only need to have the correct JVM. This gives the portability feature to bytecodes, which is one of the strength of Java.

Source code can be compiled by IDE (Eclipse, IntelliJ, etc), or by console using javac command (java compiler). In our example is:

javac SimpleApp.java

If all it’s ok, the output is a file SimpleApp.class, which contains the application bytecodes.

In order to run this application, we can use java command java SimpleApp.java, although IDE could be used directly.

I suggest to know at least basic commands, because one day these knowledge could save us hehe.

 

Loading a program into memory Phase

When a java application is executed, appears an important component called class loader. This component, loads classes dynamically into JVM, which it means as they are needed. It is useful remember that JVM is like a virtual computer, it has is own memory, although this means ultimately that it will load in physical memory.

The Class Loader will try to load our .class in memory and then will load all .class referenced by our application (SimpleApp.class in our example). Referenced classes by the application could be locally in a hard disk or could be remotelly.

All JVMs includes a loader classes, which is known as primordial loader class.

 

Bytecodes Verification Phase

In order to assure that security restrictions are not violated, as classes are loading into memory, bytecode verifier examine bytecode to ensure that are valid. Why do this? Basically Java run-time system doesn’t trust in the incoming code.

Several tests are performed, from a simple verification that format of the fragment of code is correct, as well as:

  • It doesn’t forge pointers
  • It doesn’t violate any access restriction of memory
  • It accesses objects as what they are (for example, a System object always is used as System and never as anything else)

 

Execution Phase

In this phase, JVM executes instructions specified by bytecodes loaded in its memory.

As I mentioned previously, JVM is a kind of virtual computer which resides in physical machine. It has is own virtual memory, in which java applications are loaded

.class files are loaded into virtual memory and is the virtual machine which is in charge to translate these sentences (understandable by JVM) to sentences understandable by Operative System and underlying hardware.

Graphically:

JVM to OS

JVM acts as an intermediate layer between its “machine” language (understandable by JVM) and underlying system. This makes that compiled code can be independent from the platform in which will be executed

In order to gain efficiency in the process to interpret the code understandable by JVM to code understandable by the physical machine, current JVMs uses a technique called Just in Time compilation or JIT (also known as HotSpot compiler). When bytecodes executed frequently are detected, JIT compiler translate them to physical machine language, in this way, the next time that JVM find them it will execute directly instead make the interpretation process, adding efficiency in the execution process of a java application.

 


Leave a comment

Your email address will not be published. Required fields are marked *

5 × 1 =

One thought on “Programming in Java: From writing to execution

  • fifa 18

    I used to be recommended this web site by way of my cousin. I’m now not certain whether or not this publish is written by means of him as no one else realize such particular approximately my trouble. You’re incredible! Thanks!