Java is one of the most widely used programming languages in the world, and for good reason. If you are starting your software development journey or looking to add a battle-tested language to your toolkit, Java is an excellent choice. In this tutorial, I will walk you through what Java is, why it matters, and how to write your first program. This is not a surface-level overview. I want you to walk away with a real understanding of the language and the ecosystem around it.
Java is a general-purpose, object-oriented programming language created by James Gosling and his team at Sun Microsystems in 1995. It was originally designed for interactive television, but it turned out to be too advanced for the cable TV industry at the time. The language was later repositioned for the internet, and it took off from there.
Sun Microsystems was acquired by Oracle Corporation in 2010, and Oracle has maintained and evolved the language ever since. Java now follows a six-month release cadence, with Long-Term Support (LTS) releases every two years. As of 2024, the latest LTS version is Java 21, and Java 25 is expected as the next LTS release.
The original philosophy behind Java was “Write Once, Run Anywhere” (WORA). This means you write your Java code once, compile it, and that compiled code can run on any device that has a Java Virtual Machine (JVM) installed, whether it is Windows, macOS, or Linux. This portability is one of the key reasons Java became so dominant in enterprise software.
Java has survived and thrived for nearly three decades because of a set of core features that make it reliable for production software. Here are the features that matter most from a practical standpoint:
1. Platform Independent (via the JVM)
Java source code compiles to bytecode, not machine code. This bytecode runs on the Java Virtual Machine, which is available for virtually every operating system. You do not need to recompile your code for different platforms. This is a massive advantage for enterprise applications that need to run across diverse infrastructure.
2. Object-Oriented
Everything in Java revolves around objects and classes. Java enforces object-oriented programming principles such as encapsulation, inheritance, polymorphism, and abstraction. This makes Java code naturally organized and easier to maintain as projects grow in complexity.
3. Strongly Typed
Java is a statically and strongly typed language. Every variable must be declared with a type, and the compiler catches type mismatches before the code ever runs. This may feel restrictive if you are coming from Python or JavaScript, but in professional settings it prevents entire categories of bugs from reaching production.
4. Automatic Garbage Collection
Java manages memory for you through its garbage collector. You do not need to manually allocate and free memory like in C or C++. The JVM tracks which objects are no longer referenced and reclaims that memory automatically. Understanding how the garbage collector works becomes important when you start building high-performance applications, but as a beginner, you can trust that Java handles memory responsibly.
5. Multithreaded
Java has built-in support for multithreading. You can write programs that perform multiple tasks concurrently, which is essential for modern applications that need to handle many users or process large amounts of data simultaneously. The java.util.concurrent package provides powerful tools for writing thread-safe code.
6. Secure
Java was designed with security in mind. It runs inside the JVM sandbox, which restricts what a Java program can do on the host machine. There is no direct access to memory via pointers (unlike C/C++), which eliminates a whole class of security vulnerabilities like buffer overflows. The Java Security Manager and the bytecode verifier add additional layers of protection.
7. Robust and Mature Ecosystem
Java has one of the largest ecosystems of any programming language. Build tools like Maven and Gradle, frameworks like Spring Boot, and libraries for virtually everything you can think of, from HTTP clients to machine learning. When you learn Java, you gain access to this entire ecosystem.
These three acronyms cause a lot of confusion for beginners, but they are straightforward once you understand the relationship between them.
JVM (Java Virtual Machine)
The JVM is the engine that runs your Java bytecode. It is an abstract machine that provides the runtime environment. When you run a Java program, the JVM reads the compiled .class files and executes the bytecode. Each operating system has its own JVM implementation, which is how Java achieves platform independence. Your code does not change. Only the JVM changes per platform.
JRE (Java Runtime Environment)
The JRE is the JVM plus the standard Java class libraries needed to run Java applications. If you are an end user who just needs to run Java programs, the JRE is all you need. It includes the JVM, core libraries like java.lang, java.util, and java.io, and other supporting files.
JDK (Java Development Kit)
The JDK is the JRE plus the development tools you need to write and compile Java code. It includes the Java compiler (javac), the debugger (jdb), the archiver (jar), and other utilities. If you are a developer, you need the JDK. Here is how they nest together:
JDK = JRE + Development Tools (javac, jdb, jar, etc.)
JRE = JVM + Standard Libraries
JVM = The engine that executes bytecode
In practice, since Java 11, Oracle no longer distributes a standalone JRE. When you download the JDK, you get everything you need to both develop and run Java applications.
Before you can write and run Java code, you need to install the JDK on your machine. Here are the most common approaches:
Go to Oracle’s Java Downloads or Eclipse Adoptium (the community-driven distribution) and download the latest LTS version for your operating system. Run the installer and follow the prompts.
SDKMAN is a tool for managing multiple JDK versions. It is my preferred method because it makes switching between Java versions painless.
# Install SDKMAN curl -s "https://get.sdkman.io" | bash source "$HOME/.sdkman/bin/sdkman-init.sh" # List available Java versions sdk list java # Install Java 21 (Temurin distribution) sdk install java 21.0.2-tem # Verify installation java -version
# Install the latest LTS JDK via Homebrew brew install openjdk@21 # Symlink so the system can find it sudo ln -sfn /opt/homebrew/opt/openjdk@21/libexec/openjdk.jdk \ /Library/Java/JavaVirtualMachines/openjdk-21.jdk # Verify installation java -version
Regardless of how you installed Java, open a terminal and run:
java -version
You should see output similar to:
openjdk version "21.0.2" 2024-01-16 OpenJDK Runtime Environment Temurin-21.0.2+13 (build 21.0.2+13) OpenJDK 64-Bit Server VM Temurin-21.0.2+13 (build 21.0.2+13, mixed mode)
If you see a version number, you are ready to write Java code.
Let us write the classic Hello World program and break down every part of it. Create a file called HelloWorld.java and add the following code:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
This is only five lines of code, but there is a lot happening. Let me explain each part:
public class HelloWorld
This declares a public class named HelloWorld. In Java, every piece of code lives inside a class. The public keyword means this class is accessible from anywhere. The file name must match the class name exactly, including capitalization. That is why the file is called HelloWorld.java.
public static void main(String[] args)
This is the entry point of any Java application. When you run a Java program, the JVM looks for this exact method signature. Let us break it down further:
public – The method is accessible from outside the class (the JVM needs to call it)static – The method belongs to the class itself, not to an instance of the class. The JVM calls this method without creating an objectvoid – The method does not return any valuemain – The name of the method. This is not arbitrary. The JVM specifically looks for a method named mainString[] args – An array of strings that holds any command-line arguments passed to the programSystem.out.println("Hello, World!");
This prints the text “Hello, World!” to the console followed by a new line. System is a built-in class, out is its static output stream field, and println is a method that prints a line of text. Every statement in Java ends with a semicolon.
Java is a compiled language, but it does not compile directly to machine code. Instead, the Java compiler (javac) converts your source code into bytecode, which the JVM then interprets and executes. Here is the two-step process:
Open your terminal, navigate to the directory where you saved HelloWorld.java, and run:
javac HelloWorld.java
If there are no errors, this command produces a file called HelloWorld.class in the same directory. This .class file contains the bytecode that the JVM can execute.
java HelloWorld
Notice that you use java (not javac) and you do not include the .class extension. The output will be:
Hello, World!
Here is what happened behind the scenes:
javac read your .java source file and compiled it into bytecode stored in a .class filejava launched the JVM, which loaded the HelloWorld.class filemain method and began executing itSystem.out.println sent the text to your terminalPro tip: Starting with Java 11, you can compile and run single-file programs in one step:
java HelloWorld.java
This compiles and runs the file in memory without creating a .class file on disk. It is great for quick prototyping and learning.
The Hello World program is a good starting point, but let me show you something slightly more realistic. Here is a program that accepts a name as a command-line argument and greets the user:
public class Greeter {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Usage: java Greeter <your-name>");
System.out.println("Example: java Greeter Folau");
return;
}
String name = args[0];
String greeting = buildGreeting(name);
System.out.println(greeting);
}
/**
* Builds a personalized greeting message.
*
* @param name the name of the person to greet
* @return a formatted greeting string
*/
public static String buildGreeting(String name) {
return "Hello, " + name + "! Welcome to Java programming.";
}
}
Compile and run it:
javac Greeter.java java Greeter Folau
Output:
Hello, Folau! Welcome to Java programming.
This example demonstrates several important concepts you will use constantly in Java:
args arrayString name)buildGreeting)+ operatorreturn to exit a method earlyOne of the most common questions I get from developers starting out is “Where is Java actually used?” The answer is: almost everywhere in the enterprise world. Here are the major areas:
Enterprise and Backend Systems
Java dominates enterprise backend development. Banks, insurance companies, healthcare systems, and government agencies run massive Java applications. Frameworks like Spring Boot make it straightforward to build REST APIs, microservices, and full-stack web applications. If you look at job postings for backend developers, Java consistently appears at the top of the list.
Android Development
Java was the primary language for Android app development for years. While Kotlin has become the preferred language for new Android projects, a huge amount of existing Android code is written in Java, and understanding Java is still valuable for Android developers.
Microservices
The microservices architecture pattern has become the standard for building scalable systems, and Java is one of the most popular languages for implementing it. Spring Boot, Quarkus, and Micronaut are Java frameworks specifically optimized for building lightweight, fast-starting microservices.
Big Data and Data Engineering
Many of the most important big data tools are written in Java or run on the JVM. Apache Hadoop, Apache Kafka, Apache Spark, and Elasticsearch are all Java-based. If you work in data engineering, you will encounter Java or JVM languages regularly.
Cloud and DevOps
Java applications run on every major cloud platform, including AWS, Google Cloud, and Azure. AWS Lambda supports Java natively, and tools like Docker and Kubernetes work seamlessly with Java applications. Spring Cloud provides a complete toolkit for building cloud-native Java applications.
Internet of Things (IoT)
Java’s portability makes it a natural fit for IoT devices. Java ME (Micro Edition) and frameworks like Eclipse IoT are used to build software for embedded systems, sensors, and connected devices.
With so many programming languages available today, you might wonder if Java is still worth learning. From my experience as a senior developer who has worked with multiple languages, here is why I would still recommend Java:
Job Market Demand
Java consistently ranks in the top three most in-demand programming languages globally. Enterprise companies have massive Java codebases that need to be maintained and extended. The demand for experienced Java developers remains strong, and Java roles tend to offer competitive salaries.
Mature and Stable Ecosystem
Java has been around since 1995, which means almost every problem you will encounter has already been solved by someone. There are well-established libraries, frameworks, and patterns for virtually everything. Maven Central hosts over 500,000 libraries. You are not going to be stuck Googling obscure errors with no answers.
Transferable Knowledge
Java’s syntax and concepts transfer directly to many other languages. If you learn Java well, picking up C#, Kotlin, Scala, or even TypeScript becomes significantly easier. The object-oriented principles, design patterns, and software architecture concepts you learn in Java apply universally across software development.
Modern and Evolving
Java is not the verbose, boilerplate-heavy language it was ten years ago. Modern Java (versions 17 and above) includes features like records, sealed classes, pattern matching, text blocks, and virtual threads. The language has evolved significantly while maintaining backward compatibility, which is a remarkable engineering achievement.
Strong Community
The Java community is one of the largest and most active in software development. Stack Overflow, GitHub, conferences like JavaOne and Devoxx, and countless blogs and YouTube channels provide an enormous amount of learning resources. When you get stuck, help is readily available.
Now that you understand what Java is, how to install it, and how to write and run a basic program, you are ready to dive deeper. In the next tutorial, we will cover Java Variables, where you will learn how to store and manipulate data in your programs. From there, we will build up your knowledge step by step through data types, operators, control flow, methods, classes, and beyond.
Take the time to install the JDK on your machine and run the examples from this tutorial. The best way to learn programming is by writing code, not just reading about it.