π οΈ Java Installation and Setup
Before you write your first Java program, you need to set up your machine to understand Java.
β Fun Factβ
The tool youβll install to run Java is called a JDK (Java Development Kit). Think of it as your Java toolbox with everything you need to code, compile, and run Java programs.
π» What You Needβ
To run Java code on your system:
- JDK (Java Development Kit) β for compiling and running Java.
- Text Editor or IDE β e.g., VS Code, IntelliJ IDEA, Eclipse.
π₯ Installing Java JDKβ
π Weβll install OpenJDK 21 (latest LTS) for maximum compatibility.
π΅ Windowsβ
- Go to https://jdk.java.net/
- Download the Windows .zip or installer for OpenJDK 21.
- Extract (if zip) and place it in a known location (e.g.,
C:\Java\jdk-21) - Set environment variables:
JAVA_HOME = C:\Java\jdk-21- Add
%JAVA_HOME%\bintoPath
π macOSβ
- Use Homebrew:
brew install openjdk@21
Add this to your shell config (.zshrc or .bash_profile):
```bash
export JAVA_HOME=$(/usr/libexec/java_home -v21)
export PATH=$JAVA_HOME/bin:$PATH
π§ Linux (Ubuntu/Debian)β
Run:
sudo apt update
sudo apt install openjdk-21-jdk
Verify installation:
java -version
π§ͺ Verify Installationβ
Open your terminal or command prompt and run:
java -version
You should see output like:
openjdk version "21.0.2" 2024-01-16
β Congrats! Java is installed.β
π§ Explain Like I'm Fiveβ
Imagine you're trying to speak Java, but your computer doesn't understand it yet. Installing the JDK is like giving your computer a Java dictionary and voice so it can talk Java with you!
βοΈ Real-World Analogyβ
Installing the JDK is like installing a kitchen before cooking. You need the tools (JDK), not just the recipe (Java code).
π Exerciseβ
Check Java on Your System β Run this command:
java -version
What version do you see? Write it here: _________
π§π»βπ» Extra Challengeβ
Try writing a file Hello.java with this code:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
Compile and run:
javac Hello.java
java Hello
β Whatβs Next?β
Now that Java is ready, letβs write your first Java program!
π Next: Hello World in Java