“Hello World..!” – Probably the first message that any developer tries to print on the console, while learning a new programming language.
Let’s write a basic Java program to print “Hello World..!” message on the console and go through each keyword in the program.
1 2 3 4 5 6 7 8 9 10 11 12 |
package org.sample; public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World.!!"); System.out.print("This is first program in Java. "); System.out.println("Happy Learning.!!!"); } } |
Output:
1 2 |
Hello World.!! This is first program in Java. Happy Learning.!!! |
Let’s go through each statement in the program.
1 |
package org.sample; |
This statement specifies the compiler that we are going to create a class called “HelloWorld” in the package “org.sample”.
1 |
public class HelloWorld { |
This is how a class is created in Java. “public” is a keyword and access modifier in Java that specifies this class can be visible for all classes in the other packages. “class” is another keyword that is used for creating a Class. “HelloWorld” is the class name.
1 |
public static void main(String[] args) { |
This is how a method is defined in Java.
- “public” is a keyword and access modifier in Java that specifies this method can be visible to all classes in the other packages.
- “static” is another keyword that specifies this method can be called without creating instance of the class.
- “void” is another keyword that specifies this method returns nothing.
- “main” is the name of the method. In Java, main() is the starting point of execution when a program is executed.
- “String[] args” is the parameter declaration of the method. This parameter is used for passing command line arguments to the program.
1 |
System.out.println("Hello World.!!"); |
This line displays the message “Hello World.!!” to the standard output.
- “System” is the predefined class that provides access to the system.
- “out” is the output stream that is connected to the console.
- “println()” is the built-in method which outputs the message to the console.
1 2 |
System.out.print("This is first program in Java. "); System.out.println("Happy Learning.!!!"); |
“print()” and “println()” both are built-in methods which are used for printing the message to the output stream. the only difference is
- “print()” outputs the message in the same line where as “println()” outputs the message in the new line.