Java is a widely used programming language known for its simplicity and versatility. One of the first programs beginners learn to write in any programming language is the famous "Hello, World!" program. In this article, we will explore how to write and understand this program in Java.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Let's break down the code step by step:
public class HelloWorld
: This line declares a class named HelloWorld
. In Java, every program must have at least one class defined.
public static void main(String[] args)
: This line defines the main method, which serves as an entry point for our program execution. It has a specific signature that includes the keyword static
, return type void
(indicating no value is returned), and takes an array of strings (String[]
) as arguments.
{}
: These curly braces enclose the body of the main method where we write our code statements.
System.out.println("Hello, World!");
: This statement prints out "Hello, World!" to the console using the println method from the System class provided by Java's standard library.
When you run this program, it follows these steps:
.class
file.main
.{}
), it executes each statement sequentially.System.out.println("Hello, World!");
, it outputs "Hello, World!" to the console.public
: This keyword is an access modifier that allows other classes to access this class. It means our class can be accessed from anywhere.static
: Another access modifier, it indicates that a method or variable belongs to the class itself rather than instances of the class. The main method must be static because it's called by JVM before creating any objects.void
: A return type indicating that the main method does not return any value.String[] args
: An array of strings named "args" used for command-line arguments passed when running our program.To run this program:
HelloWorld.java
.javac HelloWorld.java
HelloWorld.class
generated (the compiled bytecode).java HelloWorld
The output should appear as follows:
Hello, World!
Congratulations! You have successfully written and executed your first Java program!
In this article, we learned how to write and understand the 'Hello, World!' program in Java. We explored each component of the code and discussed its purpose within the context of Java programming language. Writing simple programs like these helps beginners grasp fundamental concepts while building confidence in their coding skills.
Now that you have taken your first step into Java programming, continue exploring more complex projects and dive deeper into its vast capabilities!
noob to master © copyleft