First Java Program
Writing a Simple "Hello, World!" Program
A basic Java program prints "Hello, World!" to the console. Here is a simple example:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Compiling and Running Java Programs
- Save the file as
HelloWorld.java
. - Compile the program using the command:
This generates ajavac HelloWorld.java
HelloWorld.class
file containing the bytecode. - Run the program using:
This executes the compiled Java class and prints "Hello, World!" to the console.java HelloWorld
Understanding the Structure of a Java Program
- Class Declaration:
public class HelloWorld
defines the class. - Main Method:
public static void main(String[] args)
serves as the program entry point. - Print Statement:
System.out.println("Hello, World!")
prints text to the console.
This basic program introduces fundamental Java concepts and serves as a foundation for more advanced topics.