Last Updated: December 5, 2025
The quintessential first program in any programming language is often "Hello, World!". This simple program serves as a gentle introduction to the syntax and structure of the language, and it’s a great way to ensure your setup is working properly.
Here's what the code looks like:
Let’s break this down:
HelloWorld. In Java, everything resides within classes.To run this program, you’ll need to:
HelloWorld.java.This creates a HelloWorld.class file, which contains the bytecode that the JVM understands.
Now, run the program with:
You should see the output:
Understanding this process is crucial because it lays the groundwork for how Java programs are structured and executed.
Now that you’ve run your first program, let’s delve deeper into the structure of a Java program. This is where you’ll start to appreciate Java's object-oriented principles and syntax rules.
Every Java program has several key components:
main method we saw earlier is a special method.int number.Here’s a more complex program that incorporates these elements:
Calculator.main method calls another method, add, which takes two integers, adds them, and returns the result.Understanding the structure helps you in the following ways:
Next up, let’s take a closer look at variables and data types. These are foundational concepts in any programming language, and Java is no exception.
Java has two main categories of data types: primitive types and reference types.
These include:
Here’s an example program that uses different primitive data types:
These include objects, strings, and arrays. For example, strings are widely used in Java:
Variables have a scope, which determines where they can be accessed. For instance, a variable declared inside a method is local to that method and cannot be accessed outside of it.
One common mistake is not initializing variables before use. Java requires that local variables be initialized before they are used. Here’s an example that would throw an error:
Understanding these nuances will help you avoid common pitfalls as you start writing more complex Java applications.
As you build more complex programs, you’ll need to control the flow of execution. Java provides several control flow statements, including conditionals and loops.
Conditionals allow your program to make decisions. The if statement is the most common:
Loops allow you to execute a block of code multiple times. The for loop and while loop are widely used.
As a developer, you will inevitably face errors, whether they are syntax errors, runtime exceptions, or logical errors. Java provides a robust way to handle errors through exceptions.
You can use try-catch blocks to gracefully handle exceptions:
Proper error handling makes your program more robust and user-friendly. Instead of crashing, your program can provide meaningful feedback and continue operating in a controlled manner.
Now that you understand how to create and run Java programs, you are ready to explore how Java works under the hood.
In the next chapter, we will look at the inner workings of the Java language, including the role of the Java Virtual Machine and how it executes your code efficiently.