In this post we will discuss and explain how to write a simple hello world program in java. To write, compile and execute a java program many IDE’s are available but we will use netbeans (www.netbeans.org). Following is the simple hello world program in java programming.
Step 1:
Open netbeans IDE, go into File and click New Project. A new box will appear. From categories select Java and from projects select Java Application and click Next.
Step 2:
Define project name and project location and make sure that Create Main Class checkbox is checked then click Finish.
Step 3:
All necessary project files will be created and main class file will be opened with following code in it.
[js]
package javaapplication2;
public class JavaApplication2 {
public static void main(String[] args) {
// TODO code application logic here
System.out.println("Hello World, I’m Learning Java");
}
}
[/js]
Step 4:
Go into Run and click Run Project (JavaApplication2) or simply press F6. It will compile the Java program and will show the output at the bottom of the code.
Code Explanation:
package javaapplication2;
The above line make sure that all project files are included in the program
public class JavaApplication2
The above line defines the main class of our project. We use class keyword to define classes in Java.
public static void main
The above line defines the main function of the class JavaApplication2. Every Java program requires at least one main function. Keyword void indicates that this function (also called as method) will not return any information. Inside parenthesis () after main keyword is the arguments list. A function may accept any number number of input arguments.
// TODO code application logic here
The above line is a single line comment in Java. Comments are not executed by Java compiler. This comment is saying that you can start the main coding of your program after this line.
System.out.println(“Hello World, I’m Learning Java”);
The above line is used in Java to output something at the output console like we use printf in C/C++ programming.
Code Output: