Hello World
Code
package com.ayokoding.cookbook.hello_world;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}Explanation
This simple Java program outputs the text “Hello World!” when it is run. Here’s a breakdown of the different parts:
package com.ayokoding.cookbook.hello_world;- This line defines the package in which this Java class is located. Packages in Java are a way to group related classes and interfaces together. They also help avoid class name conflicts in large projects. In this case,com.ayokoding.cookbook.hello_worldis the package.public class HelloWorld { }- This line declares a public class namedHelloWorld. Classes are the basic building blocks in object-oriented programming (OOP). Thepublickeyword is an access modifier which means that the class is visible to all classes everywhere, whether they are in the same package or have imported the package containing this class.public static void main(String[] args) { }- This is the main method, which is the entry point for any Java program. The Java Virtual Machine (JVM) calls this method when the program is executed.publicis an access modifier meaning that this method is accessible anywhere.staticmeans that this method belongs to theHelloWorldclass rather than an instance of the class.voidmeans this method doesn’t return any value.mainis the name of the method.String[] argsis the parameter to the main method, representing any command-line arguments.
System.out.println("Hello World!");- This line prints the string “Hello World!” to the standard output (usually the console or terminal). Here,Systemis a predefined class,outis an instance ofPrintStreamtype, which is a public and static member field of theSystemclass, andprintlnis a method ofPrintStreamclass.
So, when you run this program, it will simply print “Hello World!” to the console.
Last updated on