Java Program to display Hello World!
Objective
Write a Java program to print the text "Hello World!" to the console. This is typically the first program every beginner writes when learning a new programming language.
Algorithm / Approach
- Define a public class with any name (e.g., Hello).
- Inside the class, declare the main() method: public static void main(String[] args).
- Inside main(), call System.out.print("Hello World!") to display the text.
- Save the file with the same name as the class (Hello.java).
- Compile with javac Hello.java and run with java Hello.
Hello.java
class Hello {
public static void main(String[] a)
{
System.out.print("Hello World!");
}
}
Expected Output
Hello World!
Explanation of the Program
- The class name is
Hello. - The
main()method is the entry point of the program. System.out.print("Hello World!")prints the message to the console.- It outputs the string without a trailing newline. Use
println()if you want a newline at the end.
Complexity
Time Complexity
O(1)
Space Complexity
O(1)
Common Mistakes
- Using lowercase "system" instead of "System" — Java is case-sensitive.
- Writing "Main" as the class name but saving the file as "Hello.java" — the filename must match the class name exactly.
- Forgetting the semicolon (;) at the end of the print statement.
- Using single quotes instead of double quotes for the string literal.