Java Overview

What is Java?

Java is a high-level, object-oriented programming (OOP) language developed by Sun Microsystems (now owned by Oracle Corporation) in 1995. It was designed to be platform-independent, secure, and easy to use. Java has since become one of the most popular programming languages in the world, widely used for developing web, mobile, desktop, and enterprise applications. Java is case sensitive.

Compilation Process

Java source code is written in plain text files with a .java extension. The compilation process of Java involves several steps:

  1. Writing Code: Developers write Java code using a text editor or an Integrated Development Environment (IDE) like Eclipse, IntelliJ IDEA, etc.

  2. Compilation: The Java source code is compiled using the Java compiler (javac) into bytecode. Bytecode is a platform-independent intermediate representation of the program. This bytecode is saved in files with a .class extension.

  3. Bytecode: The generated bytecode contains instructions that can be executed by the Java Virtual Machine (JVM).

Java Virtual Machine (JVM)

The JVM is a crucial component of the Java platform. It's responsible for executing Java bytecode and providing various runtime services. Here's how it works:

  1. Class Loader Subsystem: Loads classes and interfaces as they are referenced by the Java program. It consists of three subsystems: the Bootstrap Class Loader, the Extension Class Loader, and the Application Class Loader.

  2. Runtime Data Area: JVM divides memory into several runtime data areas, including Method Area, Heap, Java Stack, PC Register, and Native Method Stack. These areas are used for different purposes such as storing class metadata, objects, and method invocation records.

  3. Execution Engine: It interprets bytecode or, in some cases, compiles it into native machine code for better performance. The execution engine includes the Just-In-Time (JIT) compiler, which dynamically compiles frequently executed bytecode into native machine code to improve execution speed.

  4. Native Interface: JVM provides a native interface to interact with native libraries and code written in other programming languages like C and C++.

  5. Garbage Collector: Manages the memory used by Java objects. It automatically deallocates memory occupied by objects that are no longer in use, preventing memory leaks and optimizing memory usage.

The JVM is available for different platforms (Windows, Linux, macOS, etc.), allowing Java programs to run on any system that has a compatible JVM installed.

Java is a statically-typed language

In a statically-typed language like Java:

  • The type of a variable is known at compile time.

  • We must explicitly declare the data type of a variable, and the type is checked by the compiler.

  • Once a variable is declared with a specific type, it cannot hold data of any other type without explicit conversion (casting).

Characteristics of Statically-Typed Java

  1. Variable Declaration: Every variable must have a type specified during declaration.

int age = 25;        // Valid: Type 'int' specified
String name = "John"; // Valid: Type 'String' specified
age = "Hello";       // Error: Type mismatch
  1. Compile-Time Type Checking: The Java compiler ensures that operations on variables are type-safe during compilation.

int x = 5.5;  // Compilation error: incompatible types
  1. Type Safety: Java ensures that you don't accidentally assign the wrong type of data to a variable, reducing bugs and runtime errors.

String str = "Hello";
str = 123;  // Compilation error: 'int' cannot be assigned to a 'String'
  1. Method Signatures Method arguments and return types must have explicit types.

public int add(int a, int b) { // Arguments and return type must be declared
    return a + b;
}

Advantages of Static Typing

  1. Early Error Detection: Errors are caught during compile-time rather than runtime, making the code safer.

  2. Code Predictability: Knowing the types makes the code easier to understand and predict.

  3. Improved Performance: Because types are known at compile time, the JVM can optimize performance better.

  4. Enhanced Tooling Support: IDEs can provide better autocomplete, refactoring tools, and error detection.

Last updated

Was this helpful?