Getting Started with Java: Hello World
Java is one of the most widely used programming languages in the world. It's known for being platform-independent, object-oriented, and secure. If you're just starting out, the best way to begin learning Java is by writing a simple program and understanding the basics like data types and variables.
Java Hello World Program:
Let’s begin with a simple "Hello World" program. This program shows the basic structure of a Java application.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
In this program, we create a class named HelloWorld. Inside the class, the main method is the entry point for the program. The line System.out.println("Hello, World!"); prints the message "Hello, World!" to the console.
To run this program, save it with the name HelloWorld.java. Then compile it using the command javac HelloWorld.java and run it with java HelloWorld.
Explanation for syntax:
public class HelloWorld {
public: This is an access modifier, which means this class can be accessed from anywhere.
class: This keyword is used to define a class in Java. A class is like a blueprint or container that holds your code.
HelloWorld: This is the name of the class. It must match the file name exactly (i.e., the file must be named HelloWorld.java).
public static void main(String[] args) {
This is the main method — the starting point of any Java program.
public: Again, this means it can be accessed from anywhere.
static: This means the method belongs to the class and can run without creating an object of the class.
void: This means the method does not return any value.
main: This is the name of the method. The Java program starts running from here.
String[] args: This is used to receive command-line arguments (like input from the user when the program starts). It's an array of strings.
System.out.println("Hello, World!");
System: This is a built-in class in Java that provides access to system-related features.
out: This is a static member of the System class. It represents the output stream (usually the console).
println: This stands for "print line", and it prints the message inside the quotes followed by a new line.
"Hello, World!": This is a string literal. It’s the message that will be printed to the screen.
So, this line prints the text Hello, World! to the console.
}
}
These are closing braces. They mark the end of the main method and the HelloWorld class.
Final Output:
When you run this code, the output will be:
Hello, World!
Conclusion:
In this blog, we explored the basic structure of a Java program using the Hello World example. You learned how a class is declared, what the main method does, and how the System.out.println statement works. Understanding this simple program is the first step toward mastering Java. As you move forward, you’ll dive deeper into variables, data types, operators, loops, and more. Stay curious, practice regularly, and soon you’ll be building powerful Java applications.