Skip to content

Chapter 1 Introduction to Java

Java is a high-level programming language developed by Sun Microsystems, which is now owned by Oracle. It was released in 1995 and designed by James Gosling.

One of the primary goals in the creation of Java was to create a language that could be used on various devices, with different operating systems. This led to one of Java's key features: Write Once, Run Anywhere (WORA). This means that once you've written your code and compiled it into bytecodes, those bytecodes can run on any device that has a Java Virtual Machine (JVM), regardless of the underlying operating system.

Features of Java

Java has numerous features that make it a popular choice for both beginner and experienced programmers. Here are some key ones:

  • Simple: Java was designed to be easy to use and takes much of its syntax from C and C++, but with a simpler object model and fewer low-level facilities.

  • Object-Oriented: Java is a fully object-oriented programming language. This means it allows you to create modular programs and reusable code.

  • Robust: Java has a strong emphasis on checking for possible errors, as it supports garbage collection, exception handling, and type checking.

  • Secure: Java enables the construction of virus-free, tamper-free systems and provides security in both compile-time and run-time environments.

  • Architecture-Neutral: No implementation-dependent features exist in Java. This makes the compilation process simpler.

  • Portable: The concept of WORA contributes to the portability of Java. Bytecodes can run on any platform having JVM.

  • High Performance: Java uses Just-In-Time (JIT) compilers to enable high performance. JIT compiles parts of the bytecodes that have similar functionality at the same time, reducing the amount of time needed for compilation.

Understanding Java Virtual Machine (JVM)

The Java Virtual Machine (JVM) is a pivotal component of the Java architecture and plays a crucial role in the execution of Java programs. It's a software implementation of a computer that executes programs like a real machine. The JVM is the platform-independent component of the Java technology, and it is this component that allows Java to fulfill its "Write Once, Run Anywhere" promise.

JVM Architecture

Understanding the architecture of the JVM is essential for grasping how Java applications run. Here's a brief breakdown of the primary components:

  • Class Loader Subsystem: This is responsible for loading class files.

  • Runtime Data Area: This is where the JVM allocates memory during the execution of a program. It includes various memory areas like Method Area, Heap Area, Stack Area, Program Counter Register, and Native Method Stack.

  • Execution Engine: This is responsible for reading and executing instructions line by line. The execution engine includes the Interpreter and the Just-In-Time (JIT) Compiler.

  • Native Method Interface (JNI): The JNI allows the Execution Engine to call or communicate with the Native Method Libraries and vice versa.

  • Native Method Libraries: This is a collection of native libraries (required for the Execution Engine) which are written in other languages like C and C++.

JVM's Role in "Write Once, Run Anywhere"

When you write and compile a Java program, the Java compiler turns your code into bytecodes, which are platform-independent instructions for the JVM. These bytecodes are stored in .class files and can be run on any device that has a JVM.

When you execute the Java program, the JVM is responsible for converting the bytecodes into machine code specific to your device's operating system and hardware, a process known as "interpretation". The JVM also utilizes a Just-In-Time (JIT) compiler for improved performance. It dynamically compiles frequently used bytecodes into machine code, which leads to a faster execution time.

In essence, the JVM allows Java's "Write Once, Run Anywhere" promise to be fulfilled by acting as a layer of abstraction between your Java code and the specific operating system and hardware of your device. This ensures that your Java programs remain portable and platform-independent.