This program displays the message “Hello World” on the console. It is usually the first program written by beginners learning Java.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Output: Hello World
public class HelloWorld {
public is an access modifier.class is the keyword used to create a Java class.HelloWorld is the name of the class.{ marks the beginning of the class body.Because this is a public class, the source file must be saved as:
HelloWorld.java
Java is case-sensitive. Therefore, HelloWorld, Helloworld, and helloworld are considered different names.
public static void main(String[] args) {
The main() method is the starting point of a Java application. When the program runs, the Java Virtual Machine starts executing the code from this method.
The main() method is declared public so the Java Virtual Machine can access it from outside the class.
The static keyword allows Java to call the main() method without creating an object of the HelloWorld class.
Without static, Java would need to create an object before starting the program.
The void keyword means the method does not return any value.
main is the method name recognized by the Java Virtual Machine as the program’s entry point.
This parameter stores command-line arguments passed to the program.
String represents textual data.[] indicates an array.args is the name of the array.For example:
java HelloWorld Java
"Java" is stored in: args[0]args can be changed, but the data type must remain a String array.For example, this is also valid: public static void main(String[] values)
System.out.println("Hello World");
This statement prints the message Hello World on the console.
SystemSystem is a predefined Java class available in the java.lang package.
The java.lang package is automatically imported into every Java program.
outout is a static output object available inside the System class. It represents the standard output stream, which normally displays information on the console.
println()println() is a method used to print information. After printing the message, it moves the cursor to the next line.
"Hello World"The text inside double quotation marks is called a string literal.
;
}
}
} closes the main() method.} closes the HelloWorld class.When the program is executed, the following steps take place:
HelloWorld.java into Java bytecode.HelloWorld.class.HelloWorld class.main() method.main().System.out.println() displays Hello World.Save the program as:
HelloWorld.java
Open Command Prompt or Terminal in the folder where the file is saved.
javac HelloWorld.java
HelloWorld.class
java HelloWorld
Do not include .java or .class while running the compiled program.
Correct:
java HelloWorld
java HelloWorld.java
print() vs println()println()System.out.println("Hello");System.out.println("World");Hello
World
println() moves the cursor to the next line after printing.
print()System.out.print("Hello ");
System.out.print("World");
print() keeps the cursor on the same line.
Class name: HelloWorld
Filename: Hello.java
class HelloWorld is public, should be declared in a file named HelloWorld.java
system.out.println("Hello World");
System.out.println("Hello World");
System.out.println("Hello World")
System.out.println("Hello World");
System.out.println('Hello World');
Correct:
System.out.println("Hello World");
The main() method is the entry point of a standalone Java application.
public static void main(String[] args)
main() method public?It is public so that the JVM can access it from outside the class.
main() method static?It is static so that the JVM can execute it without creating an object of the class.
main() void?It is void because the main() method does not return a value to the JVM.
args?Yes. The following declaration is valid:
public static void main(String[] values)
Only the variable name has changed. Its type is still String[].
Bytecode is the intermediate code produced by the Java compiler. It is stored in a .class file and executed by the JVM.
The Java compiler converts source code into bytecode. This bytecode can run on any system with a compatible JVM, supporting the idea of “Write Once, Run Anywhere.”