In Java, this program can be written as follows:
1 2 3 4 5 6 |
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } |
Let’s break down the code to understand the syntax of Java:
public class HelloWorld
: This line defines a class namedHelloWorld
. Thepublic
keyword means that this class can be accessed from anywhere in the program.public static void main(String[] args)
: This is the main method of the class. Thepublic
andstatic
keywords are used to define the visibility and scope of the method, respectively. Thevoid
keyword means that the method does not return any value. Themain
method is the entry point of the program and is executed when the program is run. TheString[] args
parameter is an array of strings that can be used to pass command-line arguments to the program.System.out.println("Hello, World!");
: This line prints the string"Hello, World!"
to the console. TheSystem.out.println
method is used to output text to the console.
In summary, this simple “Hello World” program in Java demonstrates how to define a class, create a main method, and output text to the console.