We’ll learn how to write our first Java program throughout this post. This article is only for java beginners.
hello world program will be a very simple program that will print Hello, World!
to console. You must first ensure that Java is correctly installed on your computer before running the software.
Prerequisite for running a java program
- Install the JDK if you don’t have installed it, download the JDK and install it.
- set path of the jdk/bin directory. you can follow given video
Environment Setup for writing java code
FULL SET UP VIDEO:https://www.youtube.com/watch?v=CgLXLVP4CaI
You can either write your program in an IDE such as Eclipse, intellij or in a text editor like node pad++.
Create HelloWorld class
HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Run the program, you get the following Output in the console:
Hello, World!
Every file name ends with .java. It is the file extension of java programming language.
How the above java program works
Let’s see detailed explanation of Hello world program.
Class declaration
public class HelloWorld {
Every java program should have a class definition because java is an object-oriented programming language.
- Class declaration: A class declaration includes name and visibility of the class.
class HelloWorld
is declaration of class which includes keywordclass
, followed by identifierHelloWorld
- Class declaration is followed by curly braces
{}
, which defines class’s definition.
Main method
public static void main(String[] args) {
In Java, this is referred to as the main method. This is the program’s starting point/entry point of any java programm.
- public: This is the access modifier of any class which is used to define visibility. Here main method will be visible to all other classes.
- static: static members belongs to class rather than any specific object. It simply means you can access the static members without object. As main method declare static that means we don’t have to create any object.
- void: void is another keyword which defines return type. It simply means main method does not return anything before terminating the program.
- main: main is method name here.
- String args[]: You can pass arguments to java program using args[] array. It is used to take user input using command line.
Print statement
System.out.println("Hello, World!")
System.out.println method is used to print literals in double quotes(“”) to console. As we have passed “Hello, World!” here, it will print Hello, World! to the console.
Note: Every line/statements end with semicolon(;) in java program;
Compile and run the program
If you use any IDE like eclipse, intellij you probably found run option easily, just click run the program. But if you write this program in nodepad++. Then if have to follow just two steps: 1) Compile and 2) Execute
To compile any java program, first go the appropriate folder and open command prompt. And type like:
javac HelloWorld.java
After That type:
java HelloWorld
After doing this, You get the output:
Hello, World!
Note: When we execute java program, we need to give full classname without .java
extension.
Important keynotes
Let’s go through some important points about Hello world program.
- Any java source file can have multiple classes but it can have only one public class.
- Java source file name should be same as public class name. That’s why I said above “you need to save this file as HelloWorld.java“.
- When you compile the java file, it is converted to byte code with .class extension, so you should be able to see HelloWorld.class in the directory.
- When you execute the program, JVM looks for main method and will execute it. You need to be very careful with signature of main method, otherwise program may throw java.lang.NoSuchMethodError: main.
Hope you guys enjoyed! Thank you very much! See you in the next article.