Chapter 7: First Java Program in Java

  • 4.5/5
  • 27
  • Mar 11, 2025
Java is one of the most widely used programming languages, known for its simplicity, portability, and robustness.

If you're starting with Java, the first program you typically write is "Hello, World!". This article covers everything from writing your first Java program to common errors and solutions.

Hello World - Java Program

A "Hello, World!" program in Java prints "Hello, World!" to the console. Below is the simplest version:

Output:
Hello, World!
This program serves as a starting point for understanding Java syntax and its compilation process.

To run a Java program, you need:

- JDK (Java Development Kit) - Includes compiler (javac) and runtime (java).
- JRE (Java Runtime Environment) - Required for running Java applications.
- Text Editor or IDE - Examples: Notepad, VS Code, IntelliJ IDEA, or Eclipse.
- Command Prompt or Terminal - For compiling and executing Java programs.

Creating the Hello World Example

Step 1: Writing the Java Code

Save the following code in a file named HelloWorld.java:

Step 2: Compiling the Java Program

Open a terminal or command prompt and navigate to the folder where HelloWorld.java is saved. Run:
javac HelloWorld.java
This compiles the code into a HelloWorld.class file (bytecode).

Step 3: Running the Java Program

To execute the compiled program, type:
java HelloWorld
You will see the output:
Hello, World!

Compilation Flow in Java

Java follows a two-step compilation process:

1) Compilation (javac): Converts Java source code (.java file) into bytecode (.class file).

2) Execution (java): The Java Virtual Machine (JVM) interprets the bytecode and executes the program.

Source Code (.java) → Compiler (javac) → Bytecode (.class) → JVM (java) → Execution

Parameters Used in First Java Program

Here's a breakdown of the key components in our first program:

1) public → The method is accessible from anywhere.
2) class HelloWorld → Declares a class named HelloWorld.
3) static → Allows main() to be called without creating an object.
4) void → Indicates that main() does not return any value.
5) main(String[] args) → The entry point of a Java program.
6) System.out.println() → Prints output to the console.

Different Ways to Write a Java Program

1. Changing the Order of Modifiers

Java allows changing the order of modifiers like public and static:



2. Using a Static Block

A Java program can execute without the main() method (before Java 7) using a static block:



3. Using a Different Class Name

The filename can be different from the class name, but the execution command should match the class name.
Example:

Compile: javac MyFirstProgram.java
Run: java MyFirstProgram

4. Writing a Java Program Without public Class

Java does not require the public keyword before the class.



Valid main() Method Signatures in Java

Java allows different valid forms of main():

- Using final keyword:

- Using varargs (Ellipsis ...):

- Using String args[] instead of String[] args:



Invalid main() Method Signatures

The following main() method signatures will cause errors:

Wrong return type (int instead of void)



Missing static keyword



Incorrect parameter type



Fixing "javac is not recognized as an internal or external command"

If you get the error:

'javac' is not recognized as an internal or external command, operable program or batch file.

Solution: Set the JAVA_HOME and PATH Environment Variables

1) Find your JDK installation path, e.g.,

C:\Program Files\Java\jdk-XX.X.X

2) Set JAVA_HOME variable:

- Go to Control Panel → System → Advanced system settings → Environment Variables
- Click New under System Variables
- Set Variable name: JAVA_HOME
- Set Variable value: C:\Program Files\Java\jdk-XX.X.X

3) Update the PATH variable:

Find Path under System Variables, click Edit. Add:
%JAVA_HOME%\bin
Click OK and restart the terminal.

4) Verify Installation:

Open the command prompt and run:
javac -version
If Java is properly set up, you’ll see:
javac 17.0.2
Index
Modern Java - What’s new in Java 9 to Java 17

32 min

Chapter 1: Introduction

3 min

Chapter 2: History of Java

9 min

Chapter 3: Features of Java

4 min

Chapter 4: How to Install Java?

2 min

Chapter 5: How to Set Up Java Path

2 min

Chapter 6: JDK, JVM and JRE

2 min

Chapter 7: First Java Program in Java

15 min

What is ClassLoader in Java ?

2 min

Object Oriented Programming (OOPs) Concept

17 min

Concurrency in Java: Creating and Starting a Thread

12 min

Concurrency in Java: Interrupting and Joining Threads

5 min

Concurrency in Java: Race condition, critical section, and atomic operations

13 min

Concurrency in Java: Reentrant, Read/Write and Stamped Locks

11 min

Concurrency in Java: "synchronized" and "volatile" keywords

10 min

Concurrency in Java: using wait(), notify() and notifyAll()

6 min

Concurrency in Java: What is "Semaphore" and its use?

2 min

Concurrency in Java: CompletableFuture and its use

18 min

Concurrency in Java: Producer-consumer problem using BlockingQueue

2 min

Concurrency in Java: Producer-Consumer Problem

2 min

Concurrency in Java: Thread pools, ExecutorService & Future

14 min

Java 8 Lambdas, Functional Interface & "static" and "default" methods

28 min

Method Reference in Java (Instance, Static, and Constructor Reference)

9 min

What's new in Java 21: A Tour of its Most Exciting Features

14 min

Java Memory Leaks & Heap Dumps (Capturing & Analysis)

9 min

Memory footprint of the JVM (Heap & Non-Heap Memory)

15 min