Java Hello World Program: In the last post, we get to know how we can install successfully java installation windows 10 machines and in this post, we are going to learn how to write a simple java program. As you all know that whenever we try to learn any new language, then the first program always we try to write a simple program and that a hello world program. Now our environment is ready so let’s start to write a simple java program to print hello world.
Java Hello World Program in Java
Itis a basic program so let start from a simple way, so let’s learn how to write a simple Java Hello World program in notepad and execute that program by using command prompt. So for that open the notepad and write below sample codes
package softwaretestingo.java.basics; public class HelloWorldProgram { public static void main(String[] args) { System.out.println("Hello World"); } }
After writing the code, save this file as “HelloWorldProgram.java” in any directory.
Compile and Run Java Hello World Program
To compile and run the Hello World Program goto the directory where you have stored the java program and try to build and run the java program. For compile, the program uses this code:
javac helloWorldProgram.java
As you see from the above image when I compile the program, I got an error stated that “‘javac’ is not recognized as an internal or external command, operable program or batch file.”. We got this error because we have not set the JAVA_HOME environment variable. So when we try to compile the java program by using javac command, the compiler is not able to find the javac, because of that we are getting this error. To resolve this issue, we have already written an article ” how to set java_home in windows ” you can check that.
For Run the Program:
java helloWorldProgram
Now we know how to write a java program and run a java program by using the command prompt. But let’s go a little bit deeper and try to learn the meaning of whatever code written.
public class helloWorldProgram {
It is the first statement of our Java program. Let me told you that every java program or application must have one class definition which consists of a class keyword followed by a class name.
In the above statement, we find keywords (public and class) and class name (helloWorldProgram). when we listen about keywords in Java, that keywords predefined in java with a specific purpose and utilization and most important you can’t change that you have to use that keyword as it is. The other one is the class name which you can give as you want.
Now let me told you why I have mention public and class keyword in the statement. For now, I can say that I have mentioned as public because I want others also access my program/class, so I made this class as public access modifier. In a different post, I will share more details about the access modifier. A java file can have N number of classes, but it can have only one public class, and the java file name should be the same as the public class name.
The next statement is:
public static void main(String args[]){
Let’s break the above statement so that we can easily understand it:
- Public: Public is an access modifier and by declaring public to the Main method, we can able to call the Main method from outside of the class.
- Static: By declaring the Main method as static, we can call or run the Main method without creating an object. Because the static methods are run or executed while creating an object.
- Void: the main method is not returning anything, so we declare void
- main: it is the name of the method and also the starting point from which JVM starts to run your program.
- (String[] args): This is used for receiving the arguments in the command line as strings.
Note: The Keyword Public and Static position can be changed but the Void & Main should be declared like void main. If you have mentioned the main void then you will get errors.
The Final statement is:
System.out.println("Hello World");
So let’s break this statement for better understand
- System: It is a class name
- Out: It is a static member if system class
- println: This is a method that is used to print the message in the output destinations like console or file.
I hope I can give you a clear idea about how to write the HelloWorldProgram and used statements explanation. Still, if you have any doubts, then you can drop your queries in the comment box, and I will be happy to resolve your questions.
Very nyc explanation about the very basic things of Java
Nice explanation , Great job for new learners as well as experienced peoples also