Introduction
Java Program to Print Hello World
A simple Java program to print "Hello, World!" to the console.
Problem Description
Write a Java program that prints the message "Hello, World!" to the standard output (console).
Code
public class HelloWorld {
public static void main(String[] args) {
// Print "Hello, World!" to the console
System.out.println("Hello, World!");
}
}Output
Hello, World!Explanation
public class HelloWorld: Defines a class namedHelloWorld. In Java, every application must contain a main class.public static void main(String[] args): This is the entry point of the program. The Java Virtual Machine (JVM) starts executing the program from this method.System.out.println(...): This statement prints the argument passed to it (in this case, "Hello, World!") followed by a new line to the console.
Try Yourself
Practice these variations to test your understanding.
- Print Your Name: Modify the program to print "Hello, [Your Name]!".
- Multiple Lines: Print "Hello" on the first line and "World" on the second line using two
System.out.printlnstatements. - Same Line: Print "Hello World" using two
System.out.printstatements (notice the missing 'ln'). - Pattern Printing: Print a square of asterisks (*) using 3 print statements.
*** *** *** - Brain Twister: What happens if you remove
staticfrom the main method? Try it and see the error.
