Index

Introduction to Java

Java for Beginners

1.1 Why Java? History and Use Cases

Java is one of the most influential programming languages in the world. Designed for versatility and cross-platform compatibility, it has powered innovations from web applications to mobile devices and embedded systems. To understand Java’s significance, it's essential to explore its origins, evolution, foundational principles, and its wide-ranging applications today.

Origin and Evolution of Java

Java was developed in the early 1990s by James Gosling and his team at Sun Microsystems. Originally called “Oak,” it was intended for interactive television, but the project pivoted to the web as the internet surged in popularity. In 1995, the language was officially launched as Java, with the slogan “Write Once, Run Anywhere.” This tagline encapsulated Java’s core promise: platform independence.

Java quickly rose in popularity due to its robustness, security features, and its virtual machine architecture. Over time, the Java platform evolved through major versions, each adding modern language features. From Java 1.2’s introduction of the Swing GUI toolkit to Java 8’s lambdas and streams, and Java 17’s long-term support status, the language has remained relevant and continuously modernized.

In 2010, Oracle acquired Sun Microsystems, taking over the stewardship of Java. Since then, Java has adopted a more predictable release cycle, providing developers with regular feature updates while maintaining backward compatibility.

Core Principles of Java

Java was built on several foundational principles that continue to define it:

Platform Independence

Java applications are compiled into bytecode that runs on the Java Virtual Machine (JVM), rather than directly on hardware. This means the same code can run unmodified across different operating systems.

Here’s a simple Java program that demonstrates portability by printing system information:

public class SystemInfo {
    public static void main(String[] args) {
        System.out.println("OS: " + System.getProperty("os.name"));
        System.out.println("Java Version: " +
                            System.getProperty("java.version"));
    }
}

This code, once compiled, can be executed on Windows, macOS, or Linux using any JVM—illustrating Java’s cross-platform capability.

Security

Java was designed with a strong emphasis on security. The JVM runs code within a controlled environment called the "sandbox," preventing untrusted code from accessing critical system resources. Additionally, features like automatic memory management and strong type checking reduce vulnerabilities like buffer overflows or memory leaks.

In enterprise settings, Java supports secure web application development through technologies like Java EE (now Jakarta EE) and frameworks like Spring, which include built-in protections against threats like SQL injection or cross-site scripting.

Object-Oriented Design

Java is inherently object-oriented. Its design is based on the concept of objects that encapsulate data and behavior. This leads to modular, reusable code that is easier to maintain and scale.

Real-World Applications of Java

Java’s versatility has made it a staple in several domains:

Web Development

Java has long been used for server-side development. Frameworks like Spring Boot and Jakarta EE allow developers to build scalable and secure web applications. Large-scale sites like LinkedIn and Netflix use Java for backend services due to its performance and reliability.

Android Development

Android apps are primarily written in Java (and more recently, Kotlin). Java's role in Android development has led to its widespread adoption among mobile developers. From productivity apps to mobile games, Java code powers much of the Android ecosystem.

Enterprise Applications

Java is the backbone of many enterprise systems. Its strong typing, reliability, and vast ecosystem make it ideal for banking software, customer relationship management (CRM) systems, and large-scale inventory management platforms. Tools like Apache Tomcat, JBoss, and Oracle WebLogic Server are central to Java-based enterprise development.

Embedded Systems

Java is also used in embedded systems, such as smart cards, IoT devices, and Blu-ray players. The Java ME (Micro Edition) platform is optimized for devices with limited resources, extending Java’s reach beyond traditional computing environments.

Javas Relevance Today

Despite being over two decades old, Java remains one of the most used programming languages globally. It consistently ranks high in developer surveys due to its reliability, scalability, and strong community support. The introduction of modern features like pattern matching, records, and improved concurrency in newer versions ensures that Java stays competitive with newer languages like Python or Go.

Java’s vast ecosystem—encompassing mature tools, extensive libraries, and robust community support—makes it a safe and powerful choice for beginners and professionals alike. Whether it’s building cloud-native applications, enterprise platforms, or mobile apps, Java continues to be a cornerstone of modern software development.

Index

1.2 Installing the JDK and Setting Up Your IDE

Java development begins with installing the Java Development Kit (JDK). Once installed, you can use an IDE (Integrated Development Environment) to write and run your Java code with ease.

Step 1: Download and Install the JDK

Recommended JDK version: Java SE 17 (Long-Term Support)

Download:

Go to the Oracle JDK Downloads page or use Adoptium for a free open-source version.

Install:

Step 2: Set Environment Variables (Windows & macOS Only)

Setting the JAVA_HOME variable helps tools like IDEs and build systems locate your Java installation.

Windows:

  1. Press Win + S → Search "Environment Variables".

  2. Click “Edit the system environment variables.”

  3. Under System Properties > Advanced, click Environment Variables.

  4. Click New under System Variables:

    • Name: JAVA_HOME
    • Value: Path to your JDK folder (e.g., C:\Program Files\Java\jdk-17)
  5. Add %JAVA_HOME%\bin to the Path variable.

macOS (Terminal):

echo 'export JAVA_HOME=$(/usr/libexec/java_home)' >> ~/.zprofile
source ~/.zprofile

Step 3: Verify Java Installation

Open your terminal (Command Prompt, PowerShell, or Terminal app) and run:

java -version

Expected output:

java version "17.x.x"
Java(TM) SE Runtime Environment...

Also check:

javac -version

This confirms that the Java compiler is installed.

Step 4: Install a Beginner-Friendly IDE

Here are three recommended IDEs. Choose one based on your preference.

Download:

Visit: https://www.jetbrains.com/idea/download

Setup a New Project:

  1. Open IntelliJ IDEA → Click New Project
  2. Choose Java → Select the JDK you installed (click “Add SDK” if needed).
  3. Name your project and click Create.

Add a Java class:

  1. Right-click src → New → Java Class → Name it Main
  2. Paste this code:
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, Java!");
    }
}

Run it:

Option 2: Eclipse IDE

Download:

https://www.eclipse.org/downloads/

Setup:

  1. Launch Eclipse → Select a workspace directory.
  2. Go to File > New > Java Project
  3. Right-click src → New → Class → Check “public static void main”
  4. Paste your Java code and click Run

Option 3: Visual Studio Code (with Java Extension)

Download:

https://code.visualstudio.com

Install Extensions:

  1. Open VS Code → Go to Extensions tab (Ctrl+Shift+X)
  2. Install: "Extension Pack for Java"

Create a Project:

  1. Click Java Projects (on the side bar) → Create Java Project
  2. Choose No Build Tools, name it, and select a folder.
  3. In Main.java, add:
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello from VS Code!");
    }
}
  1. Click the Run icon or right-click → Run Java

Final Output

After following the steps above, your terminal or IDE should print:

Hello, Java!

This confirms your setup is working!

Recap

Step Task
1 Install JDK
2 Set JAVA_HOME (Windows/macOS)
3 Verify Java in terminal
4 Install IDE (IntelliJ, Eclipse, or VS Code)
5 Create and run a Java program
Index

1.3 Writing Your First Java Program

Once your Java development environment is set up, it’s time to write and run your first Java programs. Java programs are made up of classes and methods, and every application must contain a main method, which is the entry point when the program runs.

Let’s walk through the structure and syntax using two small programs. You’ll learn what each part does and be encouraged to make your own changes.

Program 1: Printing a Simple Message

This is the classic first program in many languages—printing “Hello, world!” to the console.

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

Line-by-line explanation:

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

Try it out:

  1. Save the file as HelloWorld.java.
  2. Compile with: javac HelloWorld.java
  3. Run with: java HelloWorld

Expected output:

Hello, world!

🎯 Experiment: Try changing the message to your name or something else:

System.out.println("Hi, I’m learning Java!");

Program 2: A Simple Calculator

Now let’s write a Java program that performs a basic arithmetic operation and a string manipulation.

public class SimpleCalculator {
    public static void main(String[] args) {
        int a = 10;
        int b = 5;
        int sum = a + b;

        System.out.println("Sum of a and b: " + sum);

        String firstName = "Java";
        String lastName = "Learner";
        String fullName = firstName + " " + lastName;

        System.out.println("Welcome, " + fullName + "!");
    }
}

Whats happening here?

int a = 10;
        int b = 5;
int sum = a + b;
System.out.println("Sum of a and b: " + sum);
String firstName = "Java";
        String lastName = "Learner";
String fullName = firstName + " " + lastName;
System.out.println("Welcome, " + fullName + "!");

Try it out:

  1. Save as SimpleCalculator.java.
  2. Compile: javac SimpleCalculator.java
  3. Run: java SimpleCalculator

Expected output:

Sum of a and b: 15
Welcome, Java Learner!

Experiment: Try modifying:

Example:

int difference = a - b;
System.out.println("Difference: " + difference);
Index

1.4 Compiling and Running Java Code

Java is known for its "write once, run anywhere" philosophy, made possible by the Java Virtual Machine (JVM). Unlike languages that compile directly to machine code (like C++), Java code is first compiled into an intermediate format called bytecode, which is then interpreted or compiled just-in-time (JIT) by the JVM at runtime.

Let’s explore this process step by step and walk through a complete example.

Java Compilation and Execution Workflow

  1. Write source code in a .java file (human-readable).
  2. Compile it with javac, producing a .class file (bytecode).
  3. Run it using the java command, which loads the class into the JVM.

Step 1: Create a Java File

Let’s begin with a simple file called HelloWorld.java.

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

Save this file as HelloWorld.java using any plain text editor (Notepad, VS Code, etc.).

Step 2: Compile the Java File

To compile the file, use the javac (Java Compiler) command:

javac HelloWorld.java

What happens:

You can verify that the class file was created by listing the directory:

ls              # macOS/Linux
dir             # Windows

You should see:

HelloWorld.java
HelloWorld.class

What Is Bytecode?

Bytecode is an intermediate, low-level representation of your Java code. It’s not readable like source code, but it’s also not hardware-specific. The JVM can read this format and either interpret it or compile it to machine code just in time (JIT) for efficient execution.

You can inspect bytecode using the javap tool:

javap HelloWorld

This shows a disassembly of the class, revealing its structure without the source code.

Step 3: Run the Program

Now, run the compiled program using the java command:

java HelloWorld

Note: Do not include the .class extension.

Expected output:

Hello from the JVM!

Manual Compilation vs IDE Execution

Most IDEs like IntelliJ IDEA, Eclipse, or VS Code perform these steps (compile and run) behind the scenes. However, understanding what’s happening under the hood helps you debug issues and build scripts for automation.

Feature Command Line IDE
Compilation javac HelloWorld.java Handled automatically on save/build
Execution java HelloWorld One-click "Run" button
Error Reporting Shown in terminal Displayed in output/error console
Project Structure Manual setup Templates and wizards available
Learning Benefit Greater transparency and control Faster for larger projects

What Happens in the JVM

When you run java HelloWorld, the following steps occur:

  1. Class loading: The JVM loads HelloWorld.class into memory.
  2. Bytecode verification: The JVM ensures the code is safe and doesn't violate security constraints.
  3. Execution: The JVM interprets or JIT-compiles the bytecode and runs the program.

This allows your code to run the same way on Windows, macOS, or Linux without recompiling.

Try It Yourself: Practice Exercise

  1. Modify your program:

    System.out.println("Java is platform-independent!");
  2. Save, compile, and run again:

    javac HelloWorld.java
    java HelloWorld

Try creating another file:

public class Greet {
    public static void main(String[] args) {
        System.out.println("Greetings from Java!");
    }
}

Compile and run:

javac Greet.java
java Greet
Index