Skip to main content

Java

Your First Java Program: Java for Zombies

·

NetBeans IDE launch screen used to write the first Java HelloWorld program

This is part two of the Introduction to Java for Zombies series. Here you write, compile, and run your first Java program using NetBeans IDE.

All examples in this series use NetBeans on Windows 10. Download a current version from netbeans.apache.org (Apache now maintains NetBeans; the old netbeans.org redirects there). It is free.

Java programs are built from classes

Java is a pure object-oriented language. Every piece of code belongs to a class. A Java source file holds one or more class definitions, and one class must contain the main method for the program to start.

A Java source file looks like this:

class Class1 {
  // ...
}
class Class2 {
  // ...
}
class ClassN {
  public static void main(String[] args) {
    // ...
  }
}

For a program to run, it must contain a class with a main method declared as:

public static void main(String[] args)

The class concept is the foundation of object-oriented programming. You will study it thoroughly in later posts. For now, focus on the structure: one class, one main method, one program entry point.

Write, compile, and run HelloWorld in NetBeans

The first program displays "Hello World!!!!" and exits. Follow these steps exactly.

Step 1: Launch NetBeans

Double-click the NetBeans icon on the desktop to open the IDE.

Launch NetBeans IDE icon on the Windows desktop

The NetBeans home page opens.

NetBeans IDE home page showing the Start Page tab

Step 2: Create a new Java project

Go to File > New Project (or click the New File icon in the toolbar).

New File icon in the NetBeans toolbar to create a new Java project

The New Project dialog opens. Select Java in the Categories pane and Java Application under Projects. Click Next.

New Project dialog with Java selected in Categories and Java Application selected under Projects

In the Name and Location step, set Project Name to HelloWorld. NetBeans fills in the Create Main Class field automatically as helloworld.HelloWorld. Leave Set as Main Project checked, then click Finish.

New Java Application dialog with HelloWorld as project name and helloworld.HelloWorld as the main class

The project appears in the Projects tab. Open HelloWorld.java and type the following statement inside the main method:

System.out.println("Hello World!!!!");

Typing System.out.println into the HelloWorld main method in NetBeans

NetBeans checks your syntax in real time. Introduce a deliberate error, for example type voide instead of void, and the IDE highlights the problem immediately.

NetBeans syntax error highlight showing voide instead of void underlined in red

Correct the error before moving on.

Step 3: Run the program

Click the green Run button in the toolbar.

Clicking the green Run button in NetBeans to compile and execute HelloWorld

Alternatively, right-click HelloWorld in the Projects tab and choose Run.

Right-click context menu on the HelloWorld project showing the Run option

NetBeans compiles and runs the program. The Output tab shows Hello World!!!! in green text, followed by the execution time.

What each line of HelloWorld does

The program is short, but it contains 4 core Java concepts.

Class declaration

class HelloWorld {

Everything between the opening brace { and closing brace } belongs to the class HelloWorld. This file defines one class, and that class holds the main method.

The main method

public static void main(String[] args) {

Three keywords control how Java starts the program:

  • public: the method is accessible from other classes. Any main method must be public so the Java interpreter can call it.
  • static: no object of the class needs to be created before calling this method. It belongs to the class itself, not to any instance.
  • void: main returns no value.

The parameter String[] args accepts command-line arguments. In HelloWorld, none are used, but the signature is always required.

The print statement

System.out.println("Hello World!!!!");

System is a built-in Java class. Its out field is a PrintStream object. Calling println on it prints the string and moves the cursor to the next line. The alternative method print outputs the same string without the line break.

3 rules to keep in mind when writing Java code

  • Java is case-sensitive. System and system are different identifiers.
  • Every statement ends with a semicolon ;, with a few exceptions covered in later posts.
  • A single instruction can span multiple lines.

Keep reading in the Java for Zombies series

This post covers the skeleton of a Java program. The next posts in the series build on it: variables, data types, control flow, and then classes in depth.

If Java is part of a course or assignment you are working through, Java Assignment Help from working Java developers is available when you get stuck. Related posts in this series: Introduction to Java for Zombies (part one) and Exception Handling in Java (a common next topic after HelloWorld).

Share: X / Twitter LinkedIn

Related articles

  • Java

    Java Swing Tutorial for Beginners

    Learn Java Swing from scratch: build your first window, wire button events, master five layout managers, and assemble a working calculator GUI.

    May 24, 2024

  • Java

    Advanced Java Data Management Techniques

    Master advanced Java data management: optimize data structures, handle concurrent access, tune memory, and use serialization and compression in real applications.

    May 3, 2024

  • Java

    Java File I/O: Read, Write, and Manage Files

    A practical guide to Java file I/O: streams, readers and writers, NIO Path and Files, buffering, serialization, and the exceptions that break file code.

    Oct 7, 2023

← All articles

Stuck on a programming assignment?

Get expert help in Java, C++, Python, JavaScript, SQL, and more. We deliver working code with a clear walkthrough so you can understand and defend it.