Day 1: Welcome to Java
What You'll Learn Today
- What Java is and why it matters
- Setting up your development environment
- Writing your first program (Hello World)
- Understanding program structure
- Writing comments
What is Java?
Java is an object-oriented programming language developed by Sun Microsystems (now Oracle) in 1995. It was designed around the concept of "Write Once, Run Anywhere" -- meaning code written on one platform can run on any other platform that has a Java Virtual Machine.
flowchart LR
subgraph Java["Key Features of Java"]
OOP["Object-Oriented"]
Platform["Platform Independent"]
GC["Automatic Memory Management"]
Typed["Statically Typed"]
end
style OOP fill:#3b82f6,color:#fff
style Platform fill:#22c55e,color:#fff
style GC fill:#f59e0b,color:#fff
style Typed fill:#8b5cf6,color:#fff
How Java Works
Java source code is not compiled directly into machine code. Instead, it is compiled into an intermediate form called bytecode. This bytecode is then executed by the JVM (Java Virtual Machine).
flowchart LR
Source[".java file<br>Source Code"] -->|"javac<br>Compile"| Bytecode[".class file<br>Bytecode"]
Bytecode -->|"java<br>Execute"| JVM["JVM<br>Java Virtual Machine"]
JVM --> OS["OS<br>Windows / macOS / Linux"]
style Source fill:#3b82f6,color:#fff
style Bytecode fill:#8b5cf6,color:#fff
style JVM fill:#22c55e,color:#fff
style OS fill:#f59e0b,color:#fff
| Term | Description |
|---|---|
| JDK | Java Development Kit. The full set of tools needed for development |
| JRE | Java Runtime Environment. The environment needed to run Java programs |
| JVM | Java Virtual Machine. Executes bytecode |
| javac | The Java compiler |
| java | The Java execution command |
Setting Up Your Development Environment
Installing the JDK
Install the latest LTS (Long Term Support) version:
# macOS (Homebrew)
brew install openjdk@21
# Ubuntu / Debian
sudo apt install openjdk-21-jdk
# Windows (winget)
winget install EclipseAdoptium.Temurin.21.JDK
Verifying the Installation
java --version
javac --version
Expected output:
openjdk 21.0.2 2024-01-16
OpenJDK Runtime Environment (build 21.0.2+13)
OpenJDK 64-Bit Server VM (build 21.0.2+13, mixed mode)
Your First Program
Hello World
Create a file named HelloWorld.java:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Compiling and Running
# Compile
javac HelloWorld.java
# Run
java HelloWorld
Output:
Hello, World!
Important: The file name must exactly match the class name. A
HelloWorldclass must be saved asHelloWorld.java.
Program Structure
// 1. Class declaration
public class HelloWorld {
// 2. main method (entry point)
public static void main(String[] args) {
// 3. Statement
System.out.println("Hello, World!");
}
}
flowchart TB
subgraph Structure["Java Program Structure"]
Class["Class<br>public class MyApp"]
Method["main Method<br>public static void main(String[] args)"]
Statement["Statement<br>System.out.println(...)"]
end
Class --> Method --> Statement
style Class fill:#3b82f6,color:#fff
style Method fill:#22c55e,color:#fff
style Statement fill:#f59e0b,color:#fff
| Element | Description |
|---|---|
public |
Access modifier (accessible from anywhere) |
class |
Declares a class |
static |
Can be called without creating an instance |
void |
No return value |
main |
The program's entry point |
String[] args |
Command-line arguments |
Output Methods
public class OutputDemo {
public static void main(String[] args) {
// With newline
System.out.println("Hello");
System.out.println("World");
// Without newline
System.out.print("Hello ");
System.out.print("World");
System.out.println(); // Newline only
// Formatted output
String name = "Java";
int version = 21;
System.out.printf("Welcome to %s %d!%n", name, version);
}
}
| Method | Newline | Use Case |
|---|---|---|
println() |
Yes | Standard output |
print() |
No | Continuous output |
printf() |
No | Formatted output |
printf Format Specifiers
| Specifier | Type | Example |
|---|---|---|
%s |
String | "Hello" |
%d |
Integer | 42 |
%f |
Floating point | 3.14 |
%.2f |
2 decimal places | 3.14 |
%n |
Newline | - |
Comments
public class CommentDemo {
public static void main(String[] args) {
// Single-line comment
/*
* Multi-line comment
* Second line
*/
/**
* Javadoc comment
* Used for generating documentation
* @param args command-line arguments
*/
System.out.println("Comment demo");
}
}
| Comment Type | Syntax | Purpose |
|---|---|---|
| Single-line | // ... |
Brief explanation |
| Multi-line | /* ... */ |
Longer explanation |
| Javadoc | /** ... */ |
API documentation |
Handy Features in Java 21+
JShell (Interactive REPL)
$ jshell
| Welcome to JShell -- Version 21
jshell> System.out.println("Hello!")
Hello!
jshell> 2 + 3
$1 ==> 5
jshell> /exit
Tip: JShell lets you try out Java code without creating a file. It is extremely useful while learning.
Running Source Files Directly (Java 11+)
# Run directly without compiling first
java HelloWorld.java
Since Java 11, you can run a single source file directly without an explicit
javacstep.
Practice: Self-Introduction Program
public class SelfIntroduction {
public static void main(String[] args) {
String name = "Alice";
int age = 25;
String hobby = "programming";
System.out.println("=== Self Introduction ===");
System.out.printf("Name: %s%n", name);
System.out.printf("Age: %d%n", age);
System.out.printf("Hobby: %s%n", hobby);
System.out.println("=========================");
}
}
Output:
=== Self Introduction ===
Name: Alice
Age: 25
Hobby: programming
=========================
Summary
| Concept | Description |
|---|---|
| Java | A platform-independent, object-oriented language |
| JDK | Development toolkit (includes the compiler) |
| JVM | Virtual machine that executes bytecode |
javac |
Compiles source code |
java |
Runs a program |
main method |
The program's entry point |
System.out.println() |
Prints to the console |
Key Takeaways
- Java is a compiled language (source -> bytecode -> JVM execution)
- The file name must match the class name
- The
mainmethod is the entry point of every program - Use
jshellto interactively experiment with code
Exercises
Exercise 1: Basic
Write a program that displays your name, age, and favorite food.
Exercise 2: Applied
Using printf, write a program that displays a product name and its price in a formatted way, such as "Product: $19.99".
Exercise 3: Challenge
Using command-line arguments (args), write a program so that running java Greeting Alice prints "Hello, Alice!".
References
Next up: On Day 2, we'll explore Variables and Data Types. You'll learn about Java's type system and how to store and manipulate data.