The vary first program any programmer starts with is the Hello World
program.Any of the Hello World program is though very basic and simple, the core concept and foundation of that programming language reides in it.Hence, today we will look into a simple Hello World
program of java and closely look into the java buzz word System.out.println
At the beginning let us code our Hello World program first.
public class HelloWorld { public static void main(String[] args){ //prints "Hello World!" to the console. System.out.println("Hello World!"); } }
The program starts with the declaration of class
public class HelloWorld {
This class is named HelloWorld, and must be contained in a file called HelloWorld.java.This is case-sensitive and must be exactly as written in the class.
public is the access modifier for the class.It means the class is visible and accessible from any other class within the same folder or package.
class is just that, indicating this is a class declaration, or essentially an Object definition. Everything in Java is an Object. a class can contain two things: Variables and Methods. This class does not contain any variables and only one method, called main.
The { bracket opens up a code block which will contain the rest of the class until it is closed with a }.
Other Interesting Posts Serialization and Deserialization in Java with Example Convert HashMap to List in Java Sorting HashMap by Key and Value in Java Random password Generator in Java Why wait(),notify(),notifyAll() defined in object class in Java How to use Comparable and Comparator in Java with Example
public static void main(String[] args) {
This is the method main which the java compiler looks at the vary beginning in order to 'run' a program.
public again is an access modifier for the method. Any method with public modifier means it can be accessed by any instance of the class.
static defines the method as a class method, meaning it belongs to the class and not specifically tied to an instance of the class.
void is the return type of the method, and means that the method does not return anything.
main is the name of the method, again, must be all lowercase.Anything between the parentheses after main are the parameters passed to the method, in the format (DataType name, ...)
In this case, the parameter is an Array of Strings, given the name args. This is special to only the main method, and is there to handle command-line arguments passed to the program at run-time.We then have another bracket (or curly brace {) that opens a code block for this method.
//prints "Hello, World!" to the console. System.out.println("Hello, World!");
The line beginning with // is a comment and is for informational purposes only and is ignored by the compiler.
System is a reference to the built-in System object which can perform certain tasks, in this case, a subclass named out and a method called println which with print a string to the current active console. Println also adds a newline at the end.
Anything inside the parentheses after println will be sent to the console, in this case the String Hello, World! defined by being put inside quotes.
The line of code is terminated with a ;, as all lines within a code block that are not conditional statements or class/method,/span> declarations must have.
The program ends with two curly braces } that are staggered with a tab space to make it easier to see which code block they apply to.
Conclusion
I hope this article served you that you were looking for. If you have anything that you want to add or share then please share it below in the comment section.