Java Tutorial



Java Tutorial

How Do Java Programs Work?


Java is a high-level programming language with several interesting features that increase its popularity among programmers. You cannot run any Java program on a simple machine, as Java needs a translation into the machine code. Hence you need to run it on a Java compiler. 

Read on to learn more about how Java program works universally.

How Does the Java Programming Language Work?

There are two principal stages in building a Java operation: compilation and execution. Programming a Java operation begins with creating a Java program, followed by compiling it. Then, you have to load the program into the memory using the Java Virtual Machine and verify the code to execute the Java program. 

JDK(Java Development Kit), JRE (Java Runtime Environment), JVM(Java Virtual Machine), and JIT(Just in Time) are some of the specific tools that play essential roles in how Java program runs. 

Java Programming Steps

You can disintegrate and enlist the entire process of how Java code works into three generic steps, which are as follows:

  • A Java source code is necessary to run the program and must be saved with a extension. 

  • Using a compiler is mandatory as the source code must be compiled, presenting the Java bytecode that requires a program.class extension. 

  • Putting the Java bytecode through the JVM to translate all the statements into machine-level language. Positive outputs can be achieved with thorough conversions.

How Java Programs Work Internally

Let us break down a simple Java program to understand how it works internally.

public class upGradTutorials {
    public static void main(String[] args) {
        System.out.println("upGrad helps people learn programming.");

Now, let's go through each component of the above program and explain its syntax:

Class Declaration

In Java, every program starts with a class declaration. In this case, we have a class named upGrad. The public keyword indicates that the class is accessible from other classes. The class name (upGrad) must match the filename (e.g.,, as seen above).

Main Method 

The main method is the entry point of the program. It is a special method that serves as the starting point for execution. It has public and static modifiers, indicating that the JVM can access it without creating an instance of the class.

The return type is void, meaning the method returns no value. It takes an array of String objects as a parameter, allowing the program to pass command-line arguments.

Print Statement

The System.out.println() statement prints the message to the console.

System.out is a predefined PrintStream object representing the standard output stream (console). The println() method prints the specified message followed by a line break.

Now that we know more about the components of the program let us learn how the program works internally through 4 essential stages:


The Java source code ( is compiled using javac (Java compiler). The compiler checks the semantics and syntax of the program, ensuring it follows the Java language rules. If there are no compilation errors, it generates bytecode in a file named upGrad.class.

Class Loading

The JVM loads the bytecode (upGrad.class) into memory when the program is executed.

It performs various checks, such as verifying the bytecode format and checking for any security violations.

Main Method Execution

The JVM locates the main method in the upGrad class. It creates a new stack frame (activation record) for the main method on the call stack.

Printing to Console

The System.out.println() statement is executed, which writes the specified message to the console. The message "upGrad" is displayed on the console.


The main method completes execution, and the program terminates. The JVM releases the allocated resources, including memory. The JVM handles memory management, exception handling, and other runtime tasks throughout this process to ensure the program runs smoothly.

Java Program Examples

public class upGradTutorials {
    public static void main(String[] args) {
        String message = "upGrad loves programming with Java!";
        // Print the original message
        // Reverse the entire sentence
        String reversedSentence = reverseSentence(message);
        // Print the reversed sentence
    public static String reverseSentence(String sentence) {
        StringBuilder reversed = new StringBuilder();
        // Split the sentence into individual words
        String[] words = sentence.split(" ");
        // Reverse each word and append to the reversed sentence
        for (int i = words.length - 1; i >= 0; i--) {
            reversed.append(words[i]).append(" ");
        return reversed.toString().trim();

This Java program begins with the declaration of a class named upGradTutorials. Inside this class, the main method is defined. Within the main method, a string variable named message is declared and assigned the value "upGrad loves programming with Java!". This is the original message that will be printed. The program then prints the original message using the System.out.println statement, which displays the text within the double quotation marks to the console.

Next, the program calls the reverseSentence method, passing the message string as an argument. The reverseSentence method is responsible for reversing the entire sentence. It takes a string parameter named sentence and returns the reversed sentence as a string.

Inside the reverseSentence method, a StringBuilder named reversed is declared. The StringBuilder is used to build a string by appending characters efficiently. The sentence is split into individual words using the split method, which takes a delimiter as an argument. In this case, the space character (" ") is used as the delimiter to split the sentence into an array of words stored in the words variable.

A for loop is used to iterate over the words array in reverse order, starting from the last index (words.length - 1). Each word is appended to the reversed StringBuilder inside the loop, followed by a space character. After the loop finishes, the reversed StringBuilder is converted to a string using the toString method, and any leading or trailing whitespace is removed using the trim method. The reversed sentence is then returned from the reverseSentence method.

Back in the main method, the program retrieves the reversed sentence from the reverseSentence method and assigns it to the reversedSentence string variable. Finally, the program prints the reversed sentence using another System.out.println statement, displaying the reversed sentence to the console.

Here is another simple Java program that adds two numbers and prints the result:

public class upGradTutorials {
    public static void main(String[] args) {
        int num1 = 5;
        int num2 = 10;
        // Perform addition
        int sum = num1 + num2;
        // Print the result
        System.out.println("The sum of " + num1 + " and " + num2 + " is: " + sum);

In this example, the main method begins by declaring two integer variables, num1, and num2, which hold the values to be added. Next, the addition operation is performed by adding num1 and num2, and the result is stored in an integer variable named sum.

Finally, the program uses the System.out.println statement to display the result to the console. Concatenating strings and the values of num1, num2, and sum construct the message.


Java is a multi-platform, object-oriented programming language that organizes its codes around objects and classes, which helps create applications, software, IoT devices, data distribution, and so on. Execution of a Java program works in a few detailed yet simple steps that begin with writing the code and conclude when the program starts running. 

You need to possess a detailed knowledge of Java if you find interest in programming or want to make a career out of it. The Java tutorials from upGrad are excellent learning resources that can enhance your competency in Java programs and help you learn more about how Java program works internally.


1. How does Java work internally?

The Java Virtual Machine (JVM) first compiles the Java code into bytecode. Next, the JVM interprets the bytecode to perform various operations, such as memory management, exception handling, and garbage collection. This makes Java platform independent, providing an abstraction layer between the underlying operating system and the code.

2. How does Java compile its program?

In Java, compiling a program means using the programmer-readable text from the program file or the source code and converting it into bytecodes. These codes play an active role as the platform-independent instructions that support the JVM. An essential part of executing the configuration procedure to set up the Java platform is to initiate setting the Classpath.

3. What compiler is used in Java?

The predominant Java compilers include the GNU Compiler for Java or GCJ, the Java Programming Language Compiler or Javac, the Jikes, and the Eclipse Compiler for Java or ECJ. A programmer usually writes the language statements in a particular programming language, entering the lines one after another using a code editor or an IDE. This is how Java program works with compilers. 

4. What is IDE in Java?

IDE stands for Integrated Development Environment, a software application that can efficiently assist a programmer in developing software codes. An IDE improves the productivity scale of all developers by combining different capabilities in a single, easily accessible application. It can perform different functions, like testing, building, editing software, and packaging. 

5. Why is Java multithreaded?

The multithreaded feature of Java lets it perform multiple tasks together. At the same time, it improves performance and enables efficient utilization of system resources, I/O operations handling, and responsive user interfaces. 

Leave a Reply

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