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.
Output:
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.
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.javaThis compiles the code into a HelloWorld.class file (bytecode).
Step 3: Running the Java Program
To execute the compiled program, type:java HelloWorldYou 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.X2) 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%\binClick OK and restart the terminal.
4) Verify Installation:
Open the command prompt and run:javac -versionIf Java is properly set up, you’ll see:
javac 17.0.2