Series: Java Core for Beginners Lecture: 01 of 12 Topics: Environment setup · JDK/JRE/JVM · Hello World · Compiling & running Java
What Is Java and Why Learn It?
Java is a general-purpose, object-oriented programming language first released by Sun Microsystems in 1995. Today it is maintained by Oracle and remains one of the most widely used languages in the world, powering everything from Android mobile apps and enterprise banking systems to cloud microservices and scientific tools.
Key characteristics of Java
Write Once, Run Anywhere. This is Java’s most famous promise. You write your code once, compile it into an intermediate format called bytecode, and that bytecode can run on any machine that has a Java Virtual Machine (JVM) installed — whether it is Windows, macOS, or Linux.
Strongly typed. Every variable in Java must have a declared type. This catches a whole class of bugs at compile time, before your program ever runs.
Object-oriented. Java organizes code around objects — bundles of data and behavior. This makes large codebases easier to structure, maintain, and scale.
Garbage collected. Java automatically manages memory for you. Unlike C or C++, you do not need to manually allocate and free memory; the JVM handles this in the background.
Mature ecosystem. After nearly 30 years of development, Java has an enormous ecosystem of libraries, frameworks (Spring, Jakarta EE, Quarkus), and tooling. Whatever problem you are solving, there is almost certainly a well-tested library for it.
Where is Java used today?
- Backend web development — REST APIs, microservices (Spring Boot is the dominant framework)
- Android development — Android apps were originally written exclusively in Java (Kotlin, the newer alternative, runs on the same JVM)
- Enterprise systems — banking, insurance, logistics, and e-commerce platforms at scale
- Big Data — tools like Apache Hadoop and Apache Kafka are written in Java
- Embedded systems and smart cards
Java versions
Java follows a six-month release cadence. Certain releases are designated Long-Term Support (LTS), meaning they receive security and bug-fix updates for several years. As of 2024, the recommended LTS versions are Java 17 and Java 21. This series uses Java 21 for all examples, but everything in the core language fundamentals covered here applies equally to Java 17.
How Java Works — JDK, JRE, and JVM
Before writing a single line of code, it helps to understand what happens between the moment you type a program and the moment the computer executes it. Three acronyms are central to this: JVM, JRE, and JDK.
The Java Virtual Machine (JVM)
The JVM is a software-based processor — a program that reads and executes Java bytecode. It acts as an abstraction layer between your compiled program and the underlying operating system, which is why the same .class file runs identically on Windows, macOS, and Linux.
The JVM is also responsible for:
- Memory management and garbage collection
- Just-In-Time (JIT) compilation — converting frequently executed bytecode into native machine code at runtime for better performance
- Security sandboxing — restricting what bytecode can do on the host system
The Java Runtime Environment (JRE)
The JRE is the JVM plus the standard class libraries — a collection of pre-built code for common tasks like reading files, making network connections, working with dates, and much more. If you only want to run a Java application (not develop one), the JRE is all you need.
The Java Development Kit (JDK)
The JDK is the complete development toolkit. It includes:
- Everything in the JRE (JVM + class libraries)
javac— the Java compiler, which translates your.javasource files into.classbytecode filesjava— the launcher that starts the JVM and runs your programjavadoc— generates HTML documentation from your source code commentsjar— packages compiled classes into a single distributable archive- Debugging and profiling tools
As a developer, you always install the JDK. The relationship looks like this:
┌────────────────────────────────────────┐
│ JDK │
│ ┌──────────────────────────────────┐ │
│ │ JRE │ │
│ │ ┌────────────────────────────┐ │ │
│ │ │ JVM │ │ │
│ │ └────────────────────────────┘ │ │
│ │ + Standard Class Libraries │ │
│ └──────────────────────────────────┘ │
│ + javac, javadoc, jar, and tools │
└────────────────────────────────────────┘
The compilation and execution flow
YourProgram.java
│
│ javac (compiler)
▼
YourProgram.class ← bytecode (platform-independent)
│
│ java (JVM launcher)
▼
JVM reads bytecode
│
▼
Native machine code executed by your CPU
When you run javac HelloWorld.java, the compiler reads your source file and produces HelloWorld.class — a file containing bytecode instructions. When you run java HelloWorld, the JVM reads that bytecode, compiles the hot paths to native code via JIT, and executes your program.
Installing the JDK
Choosing a JDK distribution
Oracle publishes the official JDK, but several high-quality, free, open-source distributions exist:
| Distribution | Publisher | Notes |
|---|---|---|
| Eclipse Temurin | Eclipse Foundation | Recommended for most developers |
| Oracle JDK | Oracle | Free for development; licensing applies in production |
| Amazon Corretto | Amazon | Popular in AWS environments |
| Microsoft Build of OpenJDK | Microsoft | Good for Azure deployments |
For this series, use Eclipse Temurin 21 (or any JDK 21 distribution). All distributions are based on OpenJDK and behave identically for our purposes.
Installing on Windows
- Go to adoptium.net and download the Temurin 21 JDK Windows installer (
.msi). - Run the installer and follow the prompts. Make sure to check “Set JAVA_HOME variable” and “Add to PATH” during installation.
- Open Command Prompt and verify the installation:
java -version
javac -version
You should see output similar to:
openjdk version "21.0.3" 2024-04-16
OpenJDK Runtime Environment Temurin-21.0.3+9
OpenJDK 64-Bit Server VM Temurin-21.0.3+9 (build 21.0.3+9, mixed mode)
Installing on macOS
Using Homebrew (recommended):
brew install --cask temurin@21
Or download the .pkg installer from adoptium.net and run it.
Verify in Terminal:
java -version
javac -version
Installing on Linux (Debian/Ubuntu)
sudo apt update
sudo apt install temurin-21-jdk
Verify:
java -version
javac -version
Verifying JAVA_HOME
Many tools (Maven, Gradle, IDEs) rely on the JAVA_HOME environment variable pointing to your JDK installation directory. Check it:
# macOS / Linux
echo $JAVA_HOME
# Windows (Command Prompt)
echo %JAVA_HOME%
If it is empty or wrong, set it manually. On macOS/Linux, add this to your ~/.bashrc or ~/.zshrc:
export JAVA_HOME=$(/usr/libexec/java_home -v 21) # macOS
export JAVA_HOME=/usr/lib/jvm/temurin-21 # Linux (path may vary)
export PATH=$JAVA_HOME/bin:$PATH
Your First Program: Hello, World!
Let us write the traditional first program. Create a new file called HelloWorld.java and type the following:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
This program prints the text Hello, World! to the terminal. Simple as it looks, every word here has a specific meaning. Let us examine each part carefully.
Breaking down the code
public class HelloWorld
In Java, all code lives inside a class. A class is a blueprint — for now, think of it as a container that holds your program’s code. The keyword public means this class is accessible from anywhere. The name HelloWorld must exactly match the filename: HelloWorld.java. This is a strict Java rule — the filename and the public class name must be identical, including capitalization.
public static void main(String[] args)
This is the entry point of every Java application — the method (function) the JVM calls first when you run your program. Let us read it piece by piece:
public— the JVM needs to call this method from outside the class, so it must be accessible publiclystatic— this method belongs to the class itself, not to any particular object; this allows the JVM to call it without creating an instance ofHelloWorldfirstvoid— this method does not return any valuemain— this specific name is what the JVM looks for as the entry point; it must be spelled exactlymainString[] args— an array of strings representing any command-line arguments passed when running the program; you can ignore this for now
System.out.println("Hello, World!")
This is the statement that actually produces output.
System— a built-in Java class that provides access to system resourcesout— a field ofSystemrepresenting the standard output stream (your terminal)println— a method that prints the given text followed by a newline character"Hello, World!"— a string literal, the text you want to print; strings in Java are always enclosed in double quotes
The line ends with a semicolon (;). In Java, every statement must end with a semicolon. Forgetting it is one of the most common beginner mistakes.
Java naming conventions
Before moving on, note these standard conventions that every Java developer follows:
| Element | Convention | Example |
|---|---|---|
| Class names | PascalCase |
HelloWorld, BankAccount |
| Method names | camelCase |
main, getUserName |
| Variable names | camelCase |
firstName, totalAmount |
| Constants | UPPER_SNAKE_CASE |
MAX_SIZE, PI |
| Package names | all lowercase | com.example.project |
Following these conventions makes your code readable to any Java developer in the world.
Compiling and Running from the Terminal
Understanding how to compile and run Java from the command line is fundamental — it is what your IDE does behind the scenes, and knowing it helps you understand what is happening when things go wrong.
Step 1 — Navigate to your file
Open your terminal (Command Prompt on Windows, Terminal on macOS/Linux) and navigate to the folder containing HelloWorld.java:
cd /path/to/your/folder
Confirm the file is there:
ls # macOS / Linux
dir # Windows
Step 2 — Compile
Run the Java compiler:
javac HelloWorld.java
If there are no errors, the compiler produces no output — it silently creates HelloWorld.class in the same directory. List the files to confirm:
ls
# HelloWorld.class HelloWorld.java
If you made a mistake, the compiler tells you exactly which line the error is on. For example, if you forgot the semicolon at the end of the println line:
HelloWorld.java:4: error: ';' expected
System.out.println("Hello, World!")
^
1 error
Read the error message carefully: it shows the filename, the line number, and a description of the problem. Fix the error in your .java file and compile again.
Step 3 — Run
Launch the JVM and run the compiled class:
java HelloWorld
Note: you pass the class name, not the filename — no .class extension.
You should see:
Hello, World!
Passing command-line arguments
Recall the String[] args parameter in main. These are values you can pass when running the program:
public class Greet {
public static void main(String[] args) {
System.out.println("Hello, " + args[0] + "!");
}
}
Compile and run:
javac Greet.java
java Greet Alice
Output:
Hello, Alice!
args[0] is the first command-line argument. args[1] would be the second, and so on.
Single-file programs (Java 11+)
Since Java 11, you can launch a single-file program without compiling first:
java HelloWorld.java
This is useful for quick experiments. However, this approach only works for single-file programs and skips the compilation step that catches errors early. For real projects, always use javac.
Introduction to IntelliJ IDEA
Typing in a plain text editor and running javac from the terminal is valuable for learning, but for any serious development you will use an Integrated Development Environment (IDE). The IDE of choice for Java development is IntelliJ IDEA by JetBrains.
Why IntelliJ IDEA?
- Smart code completion — suggests methods, variable names, and fixes as you type
- Instant error highlighting — underlines mistakes in real time, before you compile
- Built-in debugger — lets you pause your program mid-execution and inspect values
- Refactoring tools — safely rename variables, extract methods, and restructure code
- Version control integration — Git support built in
- Run configurations — compile and run your program with one click (or Shift+F10)
Installing IntelliJ IDEA
- Go to jetbrains.com/idea
- Download the Community Edition — it is completely free and has everything you need for this series
- Run the installer and follow the prompts
Creating your first project
- Open IntelliJ IDEA and click New Project
- Select Java from the left panel
- Choose your installed JDK (Temurin 21) from the SDK dropdown; if it does not appear, click Add SDK → Download JDK and select Temurin 21
- Click Next, give your project a name (e.g.,
JavaCoreSeries), choose a location, and click Create
Creating and running HelloWorld in IntelliJ
- In the Project panel on the left, right-click the
srcfolder → New → Java Class - Type
HelloWorldand press Enter - IntelliJ creates the file with the class declaration already in place. Add the
mainmethod:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
- Click the green triangle (▶) in the gutter next to
public static void main, or press Shift+F10 - The output appears in the Run panel at the bottom:
Hello, World!
Process finished with exit code 0
Exit code 0 means the program finished successfully without errors.
Essential IntelliJ shortcuts to learn now
| Action | Windows/Linux | macOS |
|---|---|---|
| Run program | Shift+F10 |
⌃R |
| Debug program | Shift+F9 |
⌃D |
| Auto-format code | Ctrl+Alt+L |
⌘⌥L |
| Find anything | Double-tap Shift |
Double-tap Shift |
| Quick fix / suggestion | Alt+Enter |
⌥Enter |
| Comment/uncomment line | Ctrl+/ |
⌘/ |
The most important shortcut to memorize right now is Alt+Enter (Windows/Linux) or ⌥Enter (macOS). When IntelliJ underlines something in red, place your cursor on it and press this shortcut — IntelliJ will suggest how to fix it.
Summary
In this lecture you have covered the foundational layer that everything else in this series builds on.
- Java is a strongly-typed, object-oriented, garbage-collected language that runs on a virtual machine, giving it platform independence.
- The JVM executes bytecode. The JRE is the JVM plus standard libraries. The JDK is the full development toolkit including the compiler.
- You install the JDK to develop Java programs. Eclipse Temurin 21 is the recommended free distribution.
- Every Java program starts at the
mainmethod — the entry point the JVM calls first. - The workflow from the terminal is: write
.java→javaccompiles to.classbytecode →javaruns the bytecode on the JVM. - IntelliJ IDEA Community Edition is the professional-grade IDE for Java development, providing error highlighting, auto-completion, and one-click run/debug.
Exercises
Work through these exercises before moving on to Lecture 2. They are designed to solidify what you have learned rather than introduce new material.
Exercise 1 — Verify your setup Open a terminal and run java -version and javac -version. Paste the output into a text file and keep it — it is a useful record for debugging environment issues later.
Exercise 2 — Modify HelloWorld Change the HelloWorld program to print your own name instead of “World”. Recompile and run it from the terminal (not the IDE).
Exercise 3 — Multiple print statements Create a new class called AboutMe.java. Use three separate System.out.println statements to print: your name, your city, and why you are learning Java. Run it both from the terminal and from IntelliJ.
Exercise 4 — Intentional errors In your HelloWorld.java, deliberately introduce three common mistakes one at a time, observe the compiler error, then fix it: – Remove the semicolon at the end of the println line – Change main to Main (capital M) – Rename the file to Hello.java while the class is still named HelloWorld
Exercise 5 — Command-line arguments Create a class called Greet.java that takes a name as a command-line argument and prints Hello, . Run it as java Greet YourName.
Up next: Lecture 2 — Variables, Data Types & Operators — where you will learn how Java stores and manipulates data, and the full set of operators available to work with it.
