Your First Java Program
Learn how to write, compile, and execute your first Java program using the standard Java application structure.
Writing Your First Java Program
A traditional Java application starts execution from the
main() method.
The following program prints a simple message to the console.
First Java Program
public class FirstProgram {
public static void main(String[] args) {
System.out.println("Hello World CIIT....🌍!");
}
}
Understanding the Program
| Part | Explanation |
|---|---|
public
|
An access modifier that makes the class or method accessible from other classes. |
class
|
Defines a class in Java. |
FirstProgram
|
The name of the class. |
static
|
Allows the main method to be invoked without creating an object of the class. |
void
|
Indicates that the method does not return a value. |
main()
|
The traditional entry point of a Java application. |
String[] args
|
Stores command-line arguments passed to the application. |
System.out.println()
|
Prints a message followed by a new line. |
Java File Naming Rule
When a class is declared as public, the source
file name must match the public class name.
FirstProgram.java
The public class in this file is:
public class FirstProgram
Compile the Program
Open a terminal in the directory containing
FirstProgram.java and run:
javac FirstProgram.java
If compilation succeeds, Java generates a bytecode file:
FirstProgram.class
Run the Program
Execute the compiled class using the
java command:
java FirstProgram
Program Output
Hello World CIIT....🌍!
Java Program Execution Flow
FirstProgram.java
↓
javac
↓
FirstProgram.class
↓
JVM
↓
main()
↓
System.out.println()
↓
Hello World CIIT....🌍!
Printing Different Values
Java can print strings, numbers, variables, and expressions.
public class Main {
public static void main(String[] args) {
String instituteName = "CIIT Institute";
String course = "Software Development";
int version = 21;
System.out.println("InstituteName: " + instituteName);
System.out.println("Course: " + course);
System.out.println("Version: " + version);
System.out.println("2 + 3 = " + (2 + 3));
}
}
Output
InstituteName: CIIT Institute
Course: Software Development
Version: 21
2 + 3 = 5
Java Comments
Comments are used to explain code and are ignored by the compiler.
Single-Line Comment
// This is a single-line comment
System.out.println("CIIT....🤓❤️!");
Multi-Line Comment
/*
This is a
multi-line comment.
*/
System.out.println("CIIT....🤓❤️!");
Common Beginner Mistakes
- Forgetting the semicolon after a statement.
- Using the wrong class name when running the program.
- Saving a public class with a different file name.
-
Using
java HelloWorld.javaandjava HelloWorldinterchangeably without understanding the execution mode. - Forgetting to install or configure a JDK.
Important Points
-
Java source files normally use the
.javaextension. -
The Java compiler is commonly invoked using
javac. -
Compiled bytecode normally uses the
.classextension. - The JVM executes Java bytecode.
-
Traditional Java applications begin execution from
main().
Summary
A basic Java program consists of a class and a main method. The source code is compiled into bytecode and executed by the JVM. Understanding this workflow is the foundation for learning Java programming.