Tutorials

Java For Beginners

This tutorial is designed for absolute beginners in Java. We'll guide you through the process of writing and running a simple Java program on your machine.

Key Takeaways

  • You need Java installed on your computer to run Java programs.
  • Use the command line to compile and run Java code.
  • Writing Java code involves creating a class with the same name as the file.

Getting Started With Java

This tutorial assumes you have Java installed. If not, go to the Oracle downloads page or use the package manager for your OS to install the latest version of Java.

To check if Java is installed on your machine, open the command prompt or terminal and run:

java -version

If Java is correctly installed, you'll see version information, something like:

java version "11.0.18" 2026-01-30
Java(TM) SE Runtime Environment (build 11.0.18+9)
Java HotSpot(TM) 64-Bit Server VM (build 11.0.18+9, mixed mode)

Hello World in Java

With Java installed, let's write our first program. In your terminal, create a new file called HelloWorld.java.

Open this file in a text editor and add the following:

HelloWorld.java

public class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello World");
  }
}

Note that the class name HelloWorld matches the file name HelloWorld.java. Java is case-sensitive, so helloWorld would not work.

Compile and Run Your Java Program

Save your HelloWorld.java file and return to your terminal. Compile the class with:

javac HelloWorld.java

This uses the javac compiler from the Java Development Kit (JDK) to create a HelloWorld.class file, which is what the system runs.

To run your compiled Java class, execute:

java HelloWorld

If everything is set up correctly, you'll see:

Hello World

Conclusion

Congratulations! You've written, compiled, and executed your first Java program. Even though it's simple, you've learned the basics of class definition, compilation, and execution in Java.

Next, explore how Java organizes code using objects and packages to build more complex applications.

FAQ

What if I encounter a "command not found" error?

This usually means that Java isn't installed or isn't in your system's PATH. Make sure to install the JDK and follow the setup instructions for your OS.

How can I update Java on my machine?

Visit the official Oracle website for downloads or use a package manager specific to your operating system to fetch the latest version.

Do I need an IDE to write Java programs?

While an Integrated Development Environment (IDE) like IntelliJ IDEA or Eclipse simplifies writing Java programs, it isn't necessary. You can write Java in any text editor and compile and run it via the terminal.

What is the difference between JDK and JRE?

The Java Development Kit (JDK) is for developers and includes the JRE (Java Runtime Environment), plus tools to develop and test code. The JRE is needed to run Java programs.

Mastering the tech interviewWhat everyone is doing wrong in tech interviews