Skip to content

Latest commit

 

History

History
937 lines (627 loc) · 17 KB

File metadata and controls

937 lines (627 loc) · 17 KB

Java Introduction

Java is more than just a programming language. It is a language + runtime environment + standard libraries + development tools + ecosystem designed around the idea of writing software that can run across different platforms.


1. What is Java?

Java is a high-level, class-based, object-oriented programming language and software platform.

It was created at Sun Microsystems in the early 1990s and publicly introduced in 1995.

A simple Java program:

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

This looks simple, but several important concepts are hidden inside it.


2. Why was Java created?

Before Java, languages such as C and C++ were widely used.

A traditional compiled program often works like:

Source Code
     ↓
Compiler
     ↓
Machine Code
     ↓
Specific CPU / OS

That creates a portability problem.

A program compiled for one environment might need to be recompiled or modified for another.

Java introduced a different model:

Java Source
     ↓
Java Compiler
     ↓
Bytecode
     ↓
JVM
     ↓
Operating System
     ↓
Hardware

The key idea is:

Compile once to bytecode, then run that bytecode on a compatible JVM.

This is the foundation of Java's famous philosophy:

Write Once, Run Anywhere (WORA).


3. Java is both a language and a platform

This distinction is extremely important.

When someone says "Java," they may mean several things.

Java Language

The syntax and rules programmers use:

int age = 20;

if (age >= 18) {
    System.out.println("Adult");
}

JVM

The Java Virtual Machine executes Java bytecode.

Java libraries

Java provides extensive standard APIs for:

  • collections
  • files
  • networking
  • concurrency
  • dates and times
  • databases
  • security
  • input/output

JDK

The Java Development Kit provides the tools required to develop Java applications.

So:

                 JAVA
                   │
       ┌───────────┼───────────┐
       ↓           ↓           ↓
    Language      JVM       Libraries
                   │
                   ↓
              Applications

4. What makes Java different?

Java was designed around several major principles.

Platform independence

Java programs compile to bytecode rather than directly to one specific machine architecture.

Object orientation

Java organizes software around classes and objects.

Automatic memory management

Java uses garbage collection rather than requiring normal application code to manually free objects.

Strong typing

Variables have defined types:

int age = 25;
String name = "Alex";

Security

The JVM and Java platform provide mechanisms for controlled execution, type safety, runtime checks, and other security features.

Multithreading and concurrency

Java has long included built-in support for concurrent programming.


5. How a Java program actually runs

Suppose we write:

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

Save it as:

Hello.java

Then compile it:

javac Hello.java

The compiler produces:

Hello.class

The .class file contains Java bytecode.

Then:

java Hello

starts the JVM and executes the bytecode.

The complete process:

             Hello.java
                  │
                  ↓
             Java Compiler
                javac
                  │
                  ↓
             Hello.class
              Bytecode
                  │
                  ↓
                 JVM
                  │
        ┌─────────┴─────────┐
        ↓                   ↓
   Runtime services     JIT compiler
        │                   │
        └─────────┬─────────┘
                  ↓
             Native execution

6. What exactly is bytecode?

Bytecode is an intermediate instruction format designed for the JVM.

It isn't ordinary source code.

It also isn't directly the same thing as CPU machine code.

Think of it as a middle layer:

Java language
     ↓
  Bytecode
     ↓
  JVM
     ↓
CPU-specific instructions

This middle layer is one of Java's most important architectural decisions.


7. What exactly is the JVM?

JVM stands for:

Java Virtual Machine

Despite its name, it isn't a physical computer.

It is a software-defined execution environment.

The JVM provides an abstraction over the underlying operating system and hardware.

It handles things such as:

  • loading classes
  • verifying bytecode
  • executing instructions
  • memory management
  • garbage collection
  • thread management
  • runtime optimization
  • exception handling

Modern JVMs can also use Just-In-Time (JIT) compilation.


8. What is JIT compilation?

Early JVM implementations relied heavily on interpretation.

Conceptually:

Bytecode
   ↓
Interpret instruction
   ↓
Interpret instruction
   ↓
Interpret instruction

Modern JVMs are much more sophisticated.

They can identify frequently executed code—often called hot code—and compile it into optimized native machine code.

Bytecode
   ↓
JVM observes execution
   ↓
Frequently executed code
   ↓
JIT compiler
   ↓
Optimized native code

This is one reason Java applications can achieve excellent performance.


9. Java and memory management

In languages such as C, programmers often explicitly manage memory.

Java normally works differently.

You create objects:

Person p = new Person();

When an object is no longer reachable by the application, the garbage collector can eventually reclaim its memory.

Conceptually:

Object created
     ↓
Object used
     ↓
No references remain
     ↓
Garbage collector identifies it
     ↓
Memory becomes reclaimable

This eliminates many classes of manual-memory-management errors.

However, garbage collection does not mean memory management is automatic in every sense.

A Java program can still have memory problems if it accidentally keeps references to objects it no longer needs.


10. Java is strongly object-oriented—but modern Java is broader

Java was designed as an object-oriented language.

The fundamental building block is the class.

Example:

class Student {
    String name;
    int age;
}

An object can be created:

Student s = new Student();

Java's object-oriented concepts include:

Encapsulation

Keeping data and behavior together.

Inheritance

Creating relationships between classes.

Polymorphism

Allowing different implementations to be treated through a common type.

Abstraction

Representing essential behavior while hiding implementation details.

Modern Java also incorporates ideas from functional programming, especially through lambdas and Streams.

So modern Java isn't purely "old-school OOP."


11. Java's type system

Java is statically typed.

For example:

int age = 25;

The compiler knows that age is an integer.

This:

age = "Hello";

would produce a compile-time type error.

Java has primitive types such as:

byte
short
int
long
float
double
char
boolean

and reference types such as:

String
Object
ArrayList
Student

This strong type system helps catch many mistakes before the program runs.


12. Java syntax

Java's syntax was heavily influenced by C and C++.

For example:

if (age >= 18) {
    System.out.println("Adult");
}

Developers familiar with C-like languages can therefore learn Java relatively easily.

Java uses:

  • { } for blocks
  • ; to terminate many statements
  • () for method calls and conditions
  • // for single-line comments
  • /* ... */ for multi-line comments

13. The main() method

A traditional standalone Java application starts from:

public static void main(String[] args)

For example:

public class Test {
    public static void main(String[] args) {
        System.out.println("Hello");
    }
}

Let's break it down.

public

The method can be accessed by the JVM.

static

The method belongs to the class rather than requiring an object first.

void

It returns no value.

main

The conventional entry-point method name.

String[] args

An array containing command-line arguments.


14. Java's standard library

Java would not be nearly as useful without its enormous standard library.

For example:

String name = "Java";

uses the String API.

Collections:

List<String> names = new ArrayList<>();

File operations:

Files.readString(path);

Networking:

java.net

Concurrency:

java.util.concurrent

Date/time:

java.time

The standard library provides a foundation on which frameworks and applications are built.


15. Java Collections

One of Java's most important standard-library areas is the Collections Framework.

Common types include:

List
Set
Map
Queue

Examples:

List<String> names = new ArrayList<>();

Set<String> uniqueNames = new HashSet<>();

Map<String, Integer> scores = new HashMap<>();

These abstractions allow programmers to work with data structures without implementing them from scratch.


16. Exception handling

Java provides structured exception handling.

try {
    // risky operation
} catch (Exception e) {
    // handle problem
}

This separates normal program flow from error-handling logic.

Java has both:

  • checked exceptions
  • unchecked exceptions

This distinction is an important part of Java's design and has influenced many discussions about Java programming style.


17. Multithreading

Java was designed with concurrency in mind from its early days.

A Java application can execute multiple tasks concurrently.

Historically:

Application
    │
    ├── Thread 1
    ├── Thread 2
    └── Thread 3

Modern Java goes further with virtual threads, finalized in Java 21.

They allow large numbers of lightweight concurrent tasks.

This is particularly valuable for highly concurrent server applications.


18. Java editions

Historically, Java was divided into different editions.

Java SE

Java Standard Edition

The core Java platform.

Java EE

Originally J2EE, later Java EE.

Designed for enterprise applications.

It was eventually transferred to the Eclipse Foundation and became Jakarta EE.

Java ME

Java Micro Edition

Designed for constrained devices.

Today, Java SE and enterprise/server technologies are generally the most relevant areas for mainstream Java developers.


19. Java in web development

Java's web history has several stages.

Stage 1 — Applets

Browser
   ↓
Java Applet

This is now obsolete.

Stage 2 — Server-side Java

Browser
   ↓
HTTP
   ↓
Java Server
   ↓
Database

Technologies such as Servlets and JSP were important here.

Stage 3 — Modern Java backend

Today, Java is commonly used for APIs and backend systems:

Web / Mobile Client
        ↓
     HTTP/REST
        ↓
   Java Application
        ↓
    Business Logic
        ↓
      Database

Frameworks such as Spring Boot are particularly important in this ecosystem.


20. Java and databases

Java applications frequently communicate with databases.

The historical standard API is JDBC.

Conceptually:

Java Application
       ↓
      JDBC
       ↓
 JDBC Driver
       ↓
    Database

Modern applications may also use technologies such as JPA and Hibernate.


21. Java today

Modern Java is used extensively for:

  • enterprise applications
  • backend APIs
  • cloud services
  • financial systems
  • distributed systems
  • large-scale business software
  • developer tools
  • data-processing systems

It is particularly strong where organizations value:

stability + performance + maintainability + ecosystem + long-term support.


22. Major Java evolution

A simplified timeline:

1991
 ↓
Green Project
 ↓
Oak
 ↓
Java
 ↓
1995 — Public introduction
 ↓
1996 — JDK 1.0
 ↓
Java 2
 ↓
Enterprise Java
 ↓
Java 5
 ↓
Generics / Annotations / Enums
 ↓
Java 8
 ↓
Lambdas / Streams
 ↓
Java 9
 ↓
Modules
 ↓
Java 11 LTS
 ↓
Java 17 LTS
 ↓
Java 21 LTS
 ↓
Virtual Threads
 ↓
Java 25 LTS
 ↓
Modern Java

23. Java's strengths

✅ Platform independence

Bytecode can run on different JVM implementations.

✅ Mature ecosystem

Decades of libraries, frameworks, tools, and expertise.

✅ Strong type system

Many errors can be caught during compilation.

✅ Automatic memory management

Garbage collection removes much manual memory management.

✅ Excellent runtime

The JVM provides sophisticated optimization and profiling capabilities.

✅ Backward compatibility

Java strongly values compatibility with existing applications.

✅ Concurrency

Java has extensive support for concurrent and parallel programming.


24. Java's weaknesses

Java isn't perfect.

❌ Historically verbose

Older Java code can require considerable boilerplate.

❌ Memory overhead

The JVM and object-heavy programming model can consume more resources than some lower-level approaches.

❌ Garbage collection complexity

Large applications sometimes require careful GC tuning and memory analysis.

❌ Large ecosystem

There are many frameworks, libraries, build systems, and configuration choices.

❌ Startup/resource considerations

Traditional JVM applications can have higher startup and memory costs than lightweight native programs, although modern JVM technologies have improved this significantly.


25. Java vs. JavaScript — a common confusion

Despite their names:

Java ≠ JavaScript

They are different programming languages.

Java JavaScript
General-purpose language Primarily scripting/programming language for web and beyond
JVM ecosystem Browser/Node.js ecosystems
Statically typed Dynamically typed
Class-based Prototype-based historically
.java source files .js source files
Common in enterprise/backend Common in web/frontend/backend

The similar names are largely a historical marketing story rather than evidence that they are the same technology.


26. Java vs. C++

Java was influenced heavily by C++, but deliberately removed or changed several features.

For example, Java normally doesn't expose:

  • pointer arithmetic
  • manual object deallocation
  • C-style preprocessor macros
  • multiple inheritance of classes

The goal was to provide powerful object-oriented programming while reducing certain sources of complexity and memory-safety problems.


27. Java's deepest idea

If you remember only one technical concept from this entire introduction, remember this:

                 JAVA
                   ↓
             Java Compiler
                   ↓
               Bytecode
                   ↓
                  JVM
                   ↓
        Runtime + JIT + GC
                   ↓
          Operating System
                   ↓
                Hardware

Java's real innovation wasn't merely its syntax.

It was the creation of a portable managed execution environment that could evolve independently of the underlying hardware.


🎯 Final mental model

Think of Java as four layers:

┌──────────────────────────────┐
│       Your Application       │
├──────────────────────────────┤
│      Java Libraries/APIs     │
├──────────────────────────────┤
│             JVM              │
│   GC + JIT + Runtime + GC    │
├──────────────────────────────┤
│       Operating System       │
├──────────────────────────────┤
│          Hardware            │
└──────────────────────────────┘

And remember the fundamental pipeline:

Write Java → compile to bytecode → JVM executes and optimizes it.

That single idea explains a huge part of why Java exists, how Java works, and why Java has remained important for more than 30 years.