Java Hello World Program: Example and Explanation
Table of Content:
How to run a sample Java Program
class Simple{ // This is my first java program. public static void main(String args[]){ System.out.println("Hello World!"); } }
Let's look at how to save the file, compile, and run the program. Please follow the subsequent steps-
Step 1:Create a folder in any drive, I am doing in my I: drive, Name it as Java Programming
Step 2:
Open notepad and add the code as above.
from file option goto save as option
Step 3:
Save the file as: Simple.java
Step 4:
Open a command prompt window stepts
1. Press window+R
2. and then type cmd
Press Enter ( click ok button )
Step 5:
From command prompt window go to the specified working directory (I/Java Programming)where you saved the Java Programming.
I:cd Java Programming
Step 6: Type ' javac Simple.java '
and press enter to compile your code. If there are no errors in your code, the command prompt will take you to the next line (Assumption : The path variable is set).
Step 7:
After compilation of the source code Simple.java
, javac compiler create a .class
file which is also called bytecode.
Step 7:
Now, type java Simple to run your program. You will be able to see ' Hello World! ' printed on the window.
A Simple Java Program
Below Java program is a very simple and basic program in Java programming language. This Java program displays "Hello World!" in the output window. And, all syntax and commands in Java programming are case sensitive. Also, each statement should be ended with semicolon (;) which is a statement terminator.
class Simple{ // This is my first java program. public static void main(String args[]){ System.out.println("Hello World!"); } }
Hello World!
Exaplanation of above program
Let's see what is the meaning of class, public, static, void, main, String[], System.out.println().
class:
class is a keyword, It is used to declare a class in java.
Simple:
Here Simple is name of the class, For all class names the first letter should be in Upper Case. If several words are used to form a name of the class, each inner word's first letter should be in Upper Case.
{ for class:
This indicates the beginning of the Simple class.
public:
public is a keyword, It is an access modifier which represents visibility, it means it is visible to all.
static:
static is a keyword, if we declare any method as static, it is known as static method. The core advantage of static method is that there is no need to create object to invoke the static method. The main method is executed by the JVM, so it doesn't require to create object to invoke the main method. So it saves memory.
void:
void is the return type of the method, it means it doesn't return any value.
main:
main represents startup of the program.
{ for main method
This indicates the beginning of the main function.
String args[]:
String[] args is used for command line argument. We will learn it later.
System.out.println("Hello World!");
System.out.println() is used print statement.
- System is a final class name and cannot be instantiated. Therefore all its members (fields and methods) will be static, and we understand that it is an utility class. As per javadoc, “…Among the facilities provided by the System class are standard input, standard output, and error output streams; access to externally defined properties and environment variables; a means of loading files and libraries; and a utility method for quickly copying a portion of an array…”
- out is static member in System class which is instance of java.io.PrintStream. Its access specifiers are public final. This gets instantiated during startup and gets mapped with standard output console of the host. This stream is open by itself immediately after its instantiation and ready to accept data. When running a program from windows command line, it is the standard console.
- println method prints text on the screen with newline. There are multiple println methods with different arguments (overloading). Every println makes a call to print method and adds a newline. print calls write() and the story goes on like that.
- The data that is to be printed is put inside brackets. You will also notice that the words in inverted commas because they are what is called a string. Each letter is a character and a series of characters grouped together is called a string. Strings always be put between inverted commas. You must have to put a semi-colon after every command to show that it is the end of the command.
} ending of main method
This indicates the end of the main function.
} ending of class
This indicates the ending of the Simple class.