USA Independence Day Offers Are Live | Flat 20% OFF | Code: PROUD
Global Tech Council
java7 min read

Java Basics for Beginners: Write Your First Java Program

Suyash RaizadaSuyash Raizada

Java Basics for Beginners starts with one practical goal: install the JDK, write a small class, compile it with javac, and run it with java. If that sounds too simple, good. Your first win in Java is not building a web app. It is proving that your machine, editor, PATH, compiler, and runtime all agree with each other.

Java is still a serious language for back end systems, Android codebases, enterprise platforms, financial applications, and cloud services. It runs on the Java Virtual Machine, so compiled Java bytecode can run anywhere a compatible JVM is installed. That platform model is one reason Java has stayed near the top of developer surveys such as Stack Overflow's annual Developer Survey and language rankings such as the TIOBE Index.

Certified Agentic AI Expert Strip

What You Need Before Writing Java Code

You do not need a heavy setup for your first Java program. You need two things.

  • A JDK: The Java Development Kit includes the compiler, named javac, and the runtime, named java.
  • An editor: A basic text editor works. IntelliJ IDEA Community Edition, Eclipse, and NetBeans are better once you start building projects.

For beginners, install an OpenJDK 21 build because Java 21 is a Long Term Support release. Eclipse Temurin, Microsoft Build of OpenJDK, and Amazon Corretto are common choices. Oracle also provides Java downloads and the official Java SE documentation.

One opinion from teaching this to new developers: run your first program from the terminal before relying on an IDE. IDEs are useful, but they hide what the compiler is actually doing. When something breaks later, that knowledge saves you time.

Step 1: Install the JDK

After installing the JDK, open your terminal or command prompt and run:

java -version
javac -version

You should see version output for both commands. If java works but javac does not, you probably installed only a runtime, or your PATH points to the wrong location. This is a classic beginner problem.

On Windows, the JDK bin folder often looks similar to:

C:\Program Files\Java\jdk-21\bin

On macOS or Linux, package managers usually set this up for you, but not always. If the terminal says javac: command not found, fix the PATH before writing more code.

Step 2: Create Your Java File

Create a new folder called java-basics. Inside it, create a file named:

HelloWorld.java

File naming matters in Java. If a class is declared as public class HelloWorld, the file must be named HelloWorld.java. Not helloworld.java. Not Main.java. Java is strict about this, and on case-sensitive systems, capitalization matters too.

Step 3: Write Your First Java Program

Paste this code into HelloWorld.java:

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

This is the standard first Java program. It prints one line of text to the console.

What Each Line Means

  • public class HelloWorld: This defines a class named HelloWorld. Java programs are organized around classes.
  • { }: Curly braces group code into blocks. Forget one and the compiler will complain.
  • public static void main(String[] args): This is the entry point. The JVM looks for this method when your program starts.
  • System.out.println: This prints text and moves to the next line.
  • ;: Java statements usually end with a semicolon.

The phrase String[] args means the program can receive command-line arguments. You do not need to use it yet, but the method signature must be correct for a normal Java entry point.

Step 4: Compile the Program

Open your terminal in the folder containing HelloWorld.java. Run:

javac HelloWorld.java

If nothing prints, that is good. Java created a new file named:

HelloWorld.class

That .class file contains bytecode, which the JVM can run.

A Real Error Beginners Hit

If your file is named Main.java but your code says public class HelloWorld, javac may print something like this:

Main.java:1: error: class HelloWorld is public, should be declared in a file named HelloWorld.java
public class HelloWorld {
       ^
1 error

This error is not mysterious. Rename the file to HelloWorld.java, or change the class name to match the file. I have seen this trip up more beginners than inheritance, generics, or memory management combined.

Step 5: Run the Program

Now run:

java HelloWorld

Do not type java HelloWorld.class. That is another common mistake. Use the class name, not the file name.

Your output should be:

Hello, World!

You have now written, compiled, and executed your first Java program.

Java Syntax Basics You Should Learn Next

Hello World proves the setup. The next step is learning the small building blocks that appear in almost every Java program.

Variables and Data Types

Java is statically typed, which means you declare the type of a variable before using it.

int age = 30;
double price = 19.99;
boolean active = true;
String name = "Asha";

int, double, and boolean are primitive types. String is a reference type and represents text.

Operators

Operators help you calculate, compare, and combine values.

int total = 10 + 5;
boolean isAdult = age >= 18;
boolean canAccess = active && isAdult;

Notice == checks equality, while = assigns a value. Mixing those up is a rite of passage, but avoid it if you can.

Control Flow

Programs need decisions and repetition.

if (age >= 18) {
    System.out.println("Adult");
} else {
    System.out.println("Minor");
}

for (int i = 1; i <= 3; i++) {
    System.out.println(i);
}

if chooses a path. for repeats work. Learn these before touching frameworks such as Spring Boot.

Methods

Methods let you name reusable behavior.

public class Greeting {
    static void sayHello(String name) {
        System.out.println("Hello, " + name);
    }

    public static void main(String[] args) {
        sayHello("Asha");
    }
}

This example passes a name into a method. Small methods make code easier to test and read.

Why Java Uses Classes and Objects

Java is object oriented. You model data and behavior through classes and objects. A class is a blueprint. An object is a specific instance created from that blueprint.

public class Student {
    String name;
    int score;

    void printResult() {
        System.out.println(name + " scored " + score);
    }
}

Object oriented programming introduces encapsulation, inheritance, polymorphism, and abstraction. Do not try to master all four in one sitting. Start by creating simple classes that represent real things, such as Student, Book, or BankAccount.

Should You Use an IDE?

Yes, but not too early. For the first hour, use the terminal. After that, switch to IntelliJ IDEA Community Edition if you want the smoothest beginner experience. Eclipse is common in enterprise environments, and NetBeans is still a practical choice for learning standard Java projects.

An IDE gives you:

  • Syntax highlighting
  • Autocomplete
  • Refactoring tools
  • Built-in debugging
  • Project management for larger applications

Do not let autocomplete write code you do not understand. Use it as a helper, not a crutch.

Where Java Is Used in Real Work

Java shows up where long-term reliability matters. Common areas include:

  • Back end web systems: REST APIs, authentication services, and business applications.
  • Enterprise software: Banking, insurance, logistics, and ERP systems.
  • Android applications: Kotlin is now preferred for many new Android projects, but Java remains common in existing codebases.
  • Big data and streaming: Many JVM-based tools and services still require Java knowledge.
  • Cloud microservices: Java 21 virtual threads make high-concurrency service design simpler than older thread-per-request models.

That last point matters. Modern Java is not the same language many people learned in 2008. Records, switch expressions, pattern matching for instanceof, and virtual threads have made newer Java cleaner to write.

A Beginner Learning Path That Works

Use this order. It is not glamorous, but it works.

  1. Install JDK 21 and run java -version.
  2. Write and compile HelloWorld.java from the terminal.
  3. Learn variables, data types, operators, and control flow.
  4. Write a calculator and a number guessing game.
  5. Learn methods and basic debugging.
  6. Create small classes such as Student or Account.
  7. Study collections, especially ArrayList, HashMap, and HashSet.
  8. Then move to files, exceptions, unit testing, and databases.

If you are learning for work, pair hands-on practice with structured training. Use this guide as a starting point before exploring the programming courses and Java-focused certification paths on the Global Tech Council platform. If your goal is back end engineering, add SQL, Git, HTTP, and basic cloud concepts after core Java.

Common Beginner Mistakes to Avoid

  • Installing only a JRE: You need the JDK to compile code.
  • Wrong file name: public class HelloWorld belongs in HelloWorld.java.
  • Running the class file name: Use java HelloWorld, not java HelloWorld.class.
  • Ignoring compiler messages: Read the first error first. Later errors often disappear after the first fix.
  • Jumping to Spring Boot too soon: Learn Java syntax and OOP first. Frameworks reward fundamentals.

Your Next Step

Create a folder today, install JDK 21, and run the Hello World program exactly as shown. Then change the message, add a variable, and print your name with System.out.println. After that, build a small calculator. If you want a guided route, continue with a structured Java or programming course through Global Tech Council and keep every exercise runnable from your own machine.

Related Articles

View All

Trending Articles

View All