How Java Program Works?

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Overview

Java is a high-end programming language loved by many programmers for its interesting features. We cant run Java programs on a normal machine this is because high-level programming languages need to be translated into machine code first, due to this we run them on a Java compiler.

The execution of a Java program consists of five steps which are

  1. Creation of a Java Program
  2. Compiling a Java program,
  3. Loading the program into the memory by Java Virtual Machine,
  4. Java Virtual Machine verification for bytecode,
  5. Java Program execution

Introduction

When studying a programming language we need to understand the working and execution. Here we will be reading about the execution of a java program in detail and we will also understand what happens when we compile and execute a java program.

Java is a high-end programming language loved by many programmers for its interesting features. We cant run Java programs on a normal machine this is because high-level programming languages need to be translated into machine code first, due to this we run them on a Java compiler. It translates java language into the machine language making it easier for the machine to understand it. It is stated that it is a platform-independent language and does not work on the simple one-step compiling.

Now the first thing that we need to understand is how does the java programming language work? so let's have a quick look.

How Does Java Programming Language Work?

The working of the Java programming language can be summed up in three steps. Let’s go through the steps given below: How Does Java Programming Language Work

  1. Here for the first step, we need to have a java source code otherwise we won't be able to run the program you need to save it with the program.java extension.
  2. Secondly, we need to use a compiler so that it compiles the source code which in turn gives out the java bytecode and that needs to have a program.class extension. The Java bytecode is a redesigned version of the java source codes, and this bytecode can be run anywhere irrespective of the machine on which it has been built.
  3. Later on, we put the java bytecode through the Java Virtual Machine which is an interpreter that reads all the statements thoroughly step by step from the java bytecode which will further convert it to the machine-level language so that the machine can execute the code. We get the output only after the conversion is through.

So knowing about the working of java we need to understand the execution process of the program written in the language.

Execution Process of Java Program

The computer does not understand our human languages and it only understands codes that are in binary or coded form that means 0 or 1. During programming, there are a lot of steps that it undergoes-

  1. Creation of a Java Program-When we talk about creating a program or typing a code that solves any kind of problem that we may have, it means physically writing the program on any text editor. You may or may not edit the program after you have written it once. When you create a program you don't just make it and let it be the way it is you can modify it and save it on the device. But you need to save it with the java extension otherwise it would just appear as a normal text file.

Creation of a Java program

  1. Compiling a Java Program-Now once the program is created and does not have any errors or mistakes we can go ahead and compile the program when you compile a program it means the compiler compiles the program and if there are no errors after compiling the program we can further run it and get the desired output. We compile the Java program in a command prompt or another console.

Compiling a Java program 3. Loading the Program into the Memory by Java Virtual Machine- A lot of memory is required by JVM when you want to load the .class file extension before the execution. Loading is the process of placing a program in memory for it to run. The .class files are needed by the program to execute the file.

Loading the program

  1. Java Virtual Machine verification for bytecode- Jvm has a bytecode verifier that is due to maintenance of the security of the program. The bytecode verifies the code only and only after the classes have been loaded in the memory to maintain the security of the program. It makes sure that the bytecodes are valid and accessible. It also saves the computer from various viruses and unsecure websites.

  2. Java Program Execution-The above steps are executed by JVM when it interprets the bytecode. Earlier JVM's were slow and only interpreted one bytecode at a time. Nowadays the modern JVM's are much faster as they use JIT(just-in-time) compilation units. These JVMs can execute various tasks at the same time. We also call them HotSpot compilers as they are used by JVM to find out the hot spots in our bytecode. Later on, the source code is converted bytecode into machine language.

Let’s understand the tools that come in handy when we code in the java programming language.

  1. Java Development Kit : As the name formally states Java development kit is a full-time kit that has a compiler, Java Runtime Environment(JRE), Debuggers, Java documents inclusive in it. For further execution in java, we need to have JDK installed on our computers to further lead on to the creation, compilation, and running of the java program. Here, as we use JDK we need an environment to run the programs.We use JRE Java Runtime Environment which provides the least requirements to execute the java program. It provides the JVM, Core classes and supporting files.

Java Development Kit

  1. Java Virtual Machine : This generally is referred to as JVM contains three phases that we have to follow. It is a very important part not only of JDK but also JRE as it is inbuilt in both of the places. When you run a program using the JRE amd JDK it also goes to the JVM as it is required to run the java program and it also interprets the program. The phases are as follows: Compile the Code: The Java Development Kit(JDK) provides us with the JAVAC compiler to get through with this step. Run the Code: JVM runs the bytecode that is provided by the compiler.

Java Runtime Sytem

  1. Just in Time: Just in time, i.e JIT can be easily defined as a compiler that is witnessed in the manufacturing workflow and it aims directly at reducing the costs within the production systems and the flow times as well.

  2. Java Runtime Environment: JDK includes JRE. Which in turn after installation allows the java program to run. But we still can’t compile it. It has a browser, the applet supports, and a few plugins included in it. So, to run a java program on your respective computers you need JRE. JRE is made up of multiple elements altogether and they are:

  • Java virtual machine (JVM)
  • Java class libraries
  • Java class loader when our software tends to execute a particular program, it requires some environment to run in. Usually, it’s any operating system for example, Unix, Linux, Microsoft Windows, or the MacOS. Here our JRE acts as a translater and also a facilitator between the java program and the operating system.

Is Java a Compiler or Interpreter Based Language?

When we talk about compilers we mean that- A tool that converts source code into machine language making it easier for the computer to interpret it is called a compiler.

Let’s have a look at the Java interpreter- As a compiler, an interpreter is also a programming tool that converts our source code into readable(for computers) machine code but it doesn't work in the same way that the compiler does. It converts each statement going through it thoroughly before moving on to the next one and then executes it. No executor is needed for an interpreter.

hence we can conclude that we can consider java to be both compiler and interpreter based language because the source code first gets compiled into binary code and then the same binary code runs on the JVm (Java Virtual Machine), and this is mostly a software-based interpreter

So since we have completely understood how the codes in java are executed let's sum up what we just read.

Summary

  • Java is an object oriented programming language.
  • During programming, there are 5 steps that we undergo to execute our programs which are
    • Creation of a Java program
    • Compiling a Java program
    • Loading the program into the memory by Java Virtual Machine
    • Java Virtual Machine verification for bytecode, Java Program execution.
  • There are 4 tools(JDK, JVM, JRE, JIT) that help us with the smooth working of our programs.