Java: Getting Started
Everything you need to go from zero to running your first Java program: the language itself, the tools, the execution model, and the command-line workflow.
What is Java?
Java is a general-purpose, class-based, object-oriented programming language designed to have as few implementation dependencies as possible. A program written and compiled on one machine can run on any other machine that has a Java Virtual Machine installed, without recompilation. This principle, summarised as "Write Once, Run Anywhere", is what made Java the dominant language for enterprise software, Android development, and large-scale backend systems for most of the early 2000s and beyond.
Java is statically typed: every variable has a declared type, and the compiler catches type mismatches before the program ever runs. It manages memory automatically through garbage collection, which eliminates the manual allocation and deallocation errors that plagued C and C++ programs. These two properties together make Java both safer and more predictable than many of its predecessors.
Java by the numbers
Java remains one of the most widely used languages in the world, more than three decades after its creation.
- Used in over 3 billion devices worldwide
- The primary language for Android application development
- Powers the backends of banks, airlines, and e-commerce platforms
- Consistently ranks in the top 3 of the TIOBE popularity index
- Managed by the JCP (Java Community Process) with Oracle as the steward
History of Java
Java was created by James Gosling and his team at Sun Microsystems in the early 1990s. The project started under the name Oak, initially intended for interactive cable-TV systems. When that market did not take off, the team pivoted toward the rapidly growing World Wide Web. Java was publicly released in 1995 with the slogan "Write Once, Run Anywhere," and it caught on quickly because web browsers of the era could embed and execute Java applets directly in a page.
Java timeline
Java has gone through several major releases, each adding significant capabilities.
- 1991: Project Oak started internally at Sun Microsystems
- 1995: Java 1.0 publicly released; first web-capable language
- 1998: Java 2 (J2SE 1.2) introduced the Collections framework
- 2004: Java 5 added generics, annotations, and the enhanced for loop
- 2006: Sun open-sourced Java under the GPL (OpenJDK)
- 2010: Oracle acquired Sun Microsystems and took ownership of Java
- 2014: Java 8 introduced lambda expressions and the Stream API
- 2018: Oracle moved to a 6-month release cadence (Java 9 onward)
- 2024+: Java 21 and 22 bring pattern matching, virtual threads, and more
Applications of Java
Java's combination of portability, performance, and a mature ecosystem has led to its adoption across an unusually broad range of domains. Understanding where Java is actually used helps you appreciate why learning it opens so many doors.
Where Java is used
Java is not confined to any single domain. It runs everywhere from mobile phones to supercomputers.
- Enterprise applications:Banking platforms, insurance systems, ERP software (SAP, Oracle)
- Android development:Android apps are written in Java or Kotlin (which runs on the JVM)
- Web backends:Spring Boot powers REST APIs for Netflix, Amazon, LinkedIn
- Big data:Apache Hadoop and Apache Kafka are written in Java
- Scientific computing:NASA, CERN, and research labs use Java for simulations
- Desktop applications:IntelliJ IDEA, Eclipse, and NetBeans are all Java applications
- Embedded systems:Smart cards, set-top boxes, and IoT devices run Java ME
Java Features
Java was designed with a specific set of goals in mind. These are not just marketing points: each feature solves a real problem that earlier languages struggled with.
Platform Independent
Java source code compiles to bytecode, not machine code. Bytecode is a neutral intermediate format understood by the JVM on any operating system. The same .class file runs on Windows, macOS, and Linux without modification.
Object-Oriented
Everything in Java lives inside a class. The four pillars of OOP (encapsulation, inheritance, polymorphism, and abstraction) are first-class citizens of the language, encouraging modular, reusable design.
Robust
Java eliminates two of the most common sources of program crashes:
- Automatic memory management (garbage collection) removes manual malloc/free errors
- Strong type checking at both compile time and runtime catches bad assumptions early
- Array bounds checking prevents buffer overflows at the language level
Secure
Java programs run inside the JVM sandbox, which enforces access controls that prevent malicious code from reaching the underlying OS directly. Features like bytecode verification, the Security Manager, and no explicit pointers reduce the attack surface significantly.
Multithreaded
Java has built-in support for concurrent programming. The java.lang.Thread class, the synchronized keyword, and the java.util.concurrent package give you the tools to write programs that do multiple things at once, whether that means downloading a file while updating a UI or processing requests in parallel on a server.
JDK, JRE, and JVM Explained
These three acronyms confuse almost everyone at first. They are nested inside each other, and each serves a distinct purpose. Knowing the difference matters because error messages and documentation refer to all three, and installing the wrong one is one of the most common beginner mistakes.
JVM: Java Virtual Machine
The JVM is the engine that actually runs your program. It reads the .class bytecode files and either interprets them or compiles them to native machine code on the fly (Just-In-Time compilation). The JVM is the reason Java is platform independent: each operating system has its own JVM implementation, but they all speak the same bytecode language. When people say "Java is slow," they usually mean early JVM implementations before JIT compilation matured. Modern JVMs are highly competitive with native code for long-running workloads.
JRE: Java Runtime Environment
The JRE is the JVM plus the standard class libraries (java.lang, java.util, java.io, etc.) that your program depends on at runtime. If you only want to run Java programs (not develop them), the JRE is all you need. End users of a Java desktop application, for example, only need the JRE installed.
JDK: Java Development Kit
The JDK is the full developer package. It contains the JRE plus the tools you need to write and build Java programs:
- javac: the Java compiler that turns .java source files into .class bytecode
- java: the launcher that starts the JVM and runs a compiled program
- jar: creates and manages Java Archive files
- javadoc: generates HTML documentation from source code comments
- jdb: the Java debugger
As a developer, you always install the JDK. It includes everything the JRE provides, so you do not need to install both separately.
How Java Code Executes
Java's execution model is different from both compiled languages like C (which produce native machine code directly) and interpreted languages like Python (which typically read source line by line). Java sits in between: it compiles source to bytecode first, then interprets or JIT-compiles that bytecode at runtime.
The two-stage compilation process
Understanding this model explains why Java behaves the way it does and why certain errors appear at each stage.
- Stage 1 - Source to Bytecode:You run javac HelloWorld.java. The compiler reads your .java source file, checks it for syntax and type errors, and produces HelloWorld.class containing platform-neutral bytecode.
- Stage 2 - Bytecode to Execution:You run java HelloWorld. The JVM loads HelloWorld.class, verifies the bytecode is safe, and then either interprets it or compiles it to native machine instructions via JIT compilation.
- JIT Compilation:The JVM profiles which code paths run most often and compiles just those hot paths to highly optimised native code. This is why Java programs often get faster over time as they run.
Installing the JDK
Oracle publishes official JDK builds at oracle.com/java, and the OpenJDK community publishes open-source builds at adoptium.net (Eclipse Temurin). For most learners, Eclipse Temurin LTS (currently Java 21) is the recommended choice: it is free, open source, and kept up to date with security patches.
Windows installation
Step-by-step for Windows 10 and Windows 11:
- Go to adoptium.net and download the Windows .msi installer for Temurin 21 (LTS)
- Run the installer. Accept the default installation path (C:\Program Files\Eclipse Adoptium\)
- The installer sets JAVA_HOME and adds javac / java to your PATH automatically
- Open a new Command Prompt or PowerShell and run: java -version
- You should see something like: openjdk version "21.x.x"
macOS installation
The fastest path on macOS is through Homebrew:
- Install Homebrew if you do not have it: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- Run: brew install --cask temurin@21
- Verify: java -version and javac -version in Terminal
- Homebrew sets JAVA_HOME automatically via /usr/libexec/java_home
Linux installation (Ubuntu/Debian)
On Debian-based distributions, the package manager is the simplest option:
- Run: sudo apt update
- Run: sudo apt install default-jdk
- Verify: java -version and javac -version
- If you need a specific version: sudo apt install openjdk-21-jdk
- Set JAVA_HOME if needed: export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which java))))
IDE and Editor Options
Once you have the JDK installed, you need a place to write code. Your options range from lightweight text editors to full-featured IDEs. The right choice depends on how seriously you plan to use Java and how much system overhead you can tolerate.
VS Code (recommended for beginners)
VS Code is free, starts quickly, and becomes a capable Java editor through the Extension Pack for Java, published by Microsoft. Install it from the Extensions panel (Ctrl+Shift+X) by searching for 'Extension Pack for Java'. It adds syntax highlighting, IntelliSense, run/debug buttons, and Maven/Gradle support. It is the lightest option and an excellent starting point.
IntelliJ IDEA (industry standard)
IntelliJ IDEA by JetBrains is widely regarded as the most productive Java IDE available. The Community Edition is free and open source, covering all the fundamentals. The Ultimate Edition (paid, with a free student licence) adds Spring, databases, and web framework support. Most professional Java developers use IntelliJ IDEA daily.
Eclipse and NetBeans
Eclipse and NetBeans are both free, open-source IDEs that have been around since the early 2000s. Eclipse has a strong plugin ecosystem and is common in enterprise and Android (legacy) environments. NetBeans is the official Apache project and handles Java SE, Jakarta EE, and Maven projects out of the box. Both are heavier than VS Code but lighter than IntelliJ IDEA.
Online Java compilers (no installation needed)
If you want to run Java code right now without installing anything, these online environments work in any browser:
- onlinegdb.com/online_java_compiler: simple, supports stdin/stdout
- replit.com: full project support, shareable links
- jdoodle.com/online-java-compiler: fast, multiple JDK versions
- ideone.com: supports many languages, good for quick snippets
Hello World Program
Every Java program starts with a class, and every runnable Java program has exactly one entry point: the main method. The following is the smallest complete Java program that produces visible output. Every line has a purpose, and understanding it fully means you already understand the skeleton of every Java program you will ever write.
Hello World
JavaThe classic first program: print a line of text to the console.
Line-by-line breakdown
No line in the Hello World program is accidental. Each one reflects a rule of the Java language.
- public class HelloWorld:Declares a class named HelloWorld. In Java, all code lives inside a class. The keyword public means this class is accessible from anywhere.
- public static void main(String[] args):The entry point. The JVM calls this method to start the program. Every part of this signature is mandatory (explained in the next section).
- System.out.println("Hello, World!");:System is a built-in class. out is its static field of type PrintStream. println() is a method on that stream that prints text and a newline. The semicolon ends the statement.
- Curly braces {}:Delimit the body of the class and the body of the method. Unlike Python, Java does not use indentation to define structure; the braces do that job.
Understanding the main() Method
The JVM needs a specific contract to know where to start running your program. That contract is the main method, and every word in its signature is load-bearing.
Dissecting the main signature
public static void main(String[] args): each keyword has a specific role.
- public:The method must be accessible from outside the class so the JVM can call it before any objects have been created.
- static:The method must belong to the class itself, not to an instance, because the JVM starts the program without creating an object first.
- void:The method returns nothing. The JVM is not interested in a return value from main; it handles exit codes separately.
- main:The name the JVM looks for by convention. Any other name and the JVM will not find your entry point.
- String[] args:An array of Strings containing any command-line arguments passed when the program is launched. It can be empty but must be present.
Using Command-Line Arguments
JavaThe args array gives your program access to values passed at launch time.
Compiling and Running Java Programs
IDEs have run buttons that hide the compilation step, but understanding the command-line tools gives you a clear mental model of what is actually happening. Two commands are all you need.
The two commands
You compile once with javac, then run as many times as you like with java.
- javac HelloWorld.java:Compiles the source file. If there are no errors, it produces HelloWorld.class in the same directory. If there are errors, the compiler prints them with line numbers and stops.
- java HelloWorld:Starts the JVM, loads HelloWorld.class, and calls its main method. Note: you pass the class name, not the filename. Do not include .class.
Complete Compile-and-Run Workflow
JavaA simple addition program, run it and see the output instantly.
Common beginner mistakes
These errors come up constantly in the first few days of learning Java.
- Running java HelloWorld.java (with .java): the java command expects a class name, not a filename
- Forgetting to compile after editing: java still runs the old .class file if you skip javac
- Wrong directory: both javac and java must be run from the directory containing the file
- Class name mismatch: if your class is public class App but the file is app.java, the compiler rejects it
Java File Naming Rules
Java enforces a strict relationship between class names and file names. Unlike many languages where the filename is arbitrary, Java's compiler and class loader depend on this naming convention to locate classes. Violating it produces a compiler error, not just a warning.
The rules
These are enforced by the compiler, not style guidelines.
- One public class per file:A .java file may contain multiple classes, but at most one can be declared public.
- Filename must match the public class name exactly:If your public class is named BankAccount, the file must be BankAccount.java, not bankaccount.java or bank_account.java.
- Case sensitivity matters:Java filenames are case-sensitive on Linux and macOS. HelloWorld.java and helloworld.java are different files.
- Extension is always .java:The source extension is .java. After compilation the class file has extension .class.
- Convention for class names:Use PascalCase (also called UpperCamelCase): CustomerOrder, DatabaseConnection, HttpClient. This is a strong community convention followed universally.
File Naming in Practice
JavaA single file can have multiple classes, but only one can be public. In this compiler the public class must be named Main.
Quiz - Test Your Knowledge
Ten questions covering every concept in this tutorial. Work through each one carefully before selecting an answer.
Knowledge Check
1. Who created Java, and in which year was it publicly released?
2. What does the JVM do with Java bytecode?
3. Which component do you need to compile and run Java programs from the command line?
4. Which command compiles a Java source file called HelloWorld.java?
5. What is the correct signature of the Java entry-point method?
6. Why is Java described as "platform independent"?
7. If your public class is named Calculator, what must the file be named?
8. Which statement about the JRE is correct?
9. What does System.out.println() do in Java?
10. Which of the following is a valid online Java compiler?