Introduction to Java and JVM

Tutorial 1 of 5

Introduction to Java and JVM

1. Introduction

This tutorial aims to introduce you to the Java programming language and the Java Virtual Machine (JVM). By the end of this tutorial, you will have a solid understanding of Java's history, its principles, and how code is executed within the JVM.

You will learn:
- The basics of Java and JVM
- How to write, compile, and run a simple Java program
- How JVM executes Java code

Prerequisites:
- Basic understanding of programming concepts like variables, data types, loops, and functions
- A computer with any operating system
- Java Development Kit (JDK) installed on your system

2. Step-by-Step Guide

Java and Its History

Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. It was originally developed by James Gosling at Sun Microsystems (which is now a subsidiary of Oracle Corporation) and released in 1995.

Java follows the "write once, run anywhere" principle. Once a Java code is written, it can run on all platforms that support Java without the need for recompilation.

Java Virtual Machine (JVM)

The Java Virtual Machine (JVM) is an abstract computing machine that enables a computer to run a Java program. JVM is platform-independent, meaning you can run it on any operating system.

When you compile a Java program, it gets converted into bytecode. This bytecode runs on the JVM and gets translated into machine code.

3. Code Examples

Example 1: Hello World in Java

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
  • public class HelloWorld: This line starts the definition of a new class named HelloWorld.
  • public static void main(String[] args): This line defines the main method of the class. This is the starting point for the JVM to begin execution of a Java program.
  • System.out.println("Hello, World!");: This line prints the text "Hello, World!" to the console.

Expected output:

Hello, World!

4. Summary

In this tutorial, we introduced the Java programming language and the Java Virtual Machine (JVM). We discussed the history of Java, its principles, and how JVM executes Java code.

To further your understanding, you can explore more about Java's object-oriented programming features, exception handling, and collection framework.

5. Practice Exercises

Exercise 1: Write a Java program to print "Hello, [Your Name]!".
Solution:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, [Your Name]!");
    }
}

Exercise 2: Write a Java program that adds two numbers.
Solution:

public class Addition {
    public static void main(String[] args) {
        int num1 = 10;
        int num2 = 20;
        int sum = num1 + num2;
        System.out.println("The sum is: " + sum);
    }
}

Exercise 3: Write a Java program that calculates the factorial of a number.
Solution:

public class Factorial {
    public static void main(String[] args) {
        int num = 5;
        int factorial = 1;
        for(int i = 1; i <= num; ++i)
        {
            factorial *= i;
        }
        System.out.println("Factorial of " + num + " = " + factorial);
    }
}

Keep practicing to get more proficient in Java. Happy coding!