Java Hello World Program
First Java Program
You need the following 2 software to create your first Java Program
- The Java SE Development Kit
Please refer our last tutorial to download and install JDK
- A Text Editor
In this Java Hello World example, we’ll use Notepad. It is a simple editor included with the Windows Operating System. You can use a different text editor like NotePad++ or use online java compiler.
Hello World Java – Your First Java Program Video
This video will help you learn how to start a Java program:
Click here if the video is not accessible
Steps to Compile and Run Hello World Program in Java
Here is a step by step process on how to run Java program:
Step 1) Open Notepad from Start menu by selecting Programs > Accessories > Notepad.
Step 2) Create a Source Code for your Hello World program in Java
- Declare a class with name A.
- Declare the main method public static void main(String args[]){
- Now Type the System.out.println(“Hello World”); which will print Hello World in Java.
class A { public static void main(String args[]){ System.out.println("Hello World"); } }
Step 3) Save the file for Java Hello World program as FirstProgram.java make sure to select file type as all files while saving the file in our working folder C:\workspace
Step 4) Open the command prompt. Go to Directory C:\workspace. Compile the code of your Hello world Java program using command,
javac FirstProgram.java
Step 5) If you look in your working folder, you can see that a file named A.class has been created.
Step 6) To execute the code, enter the command java followed by the class name, as expected output Hello World is displayed now.
java A
Note: Java is case sensitive Programming language. All code, commands, and file names should is used in consistent casing. FirstProgram is not same as firstprogram.
Step 7) If you copy and paste the same code in IDE like Eclipse the compiling and execution is done with the click of a button Using IDE is convenient and improves your efficiency but since you are learning Java, we recommend you stick to notepad for simple Java program execution.
What is Command Line Argument in Java?
Command Line Argument in Java is the information that is passed to the program when it is executed. The information passed is stored in the string array passed to the main() method and it is stored as a string. It is the information that directly follows the program’s name on the command line when it is running.
Example
While running a class Demo, you can specify command line arguments as
java Demo arg1 arg2 arg3 …
Command Line Arguments in Java: Important Points
- Command Line Arguments can be used to specify configuration information while launching your application.
- There is no restriction on the number of java command line arguments. You can specify any number of arguments
- Information is passed as Strings.
- They are captured into the String args of your main method
Example: To Learn java Command Line Arguments
Step 1) Copy the following code into an editor.
class Demo{ public static void main(String args[]){ System.out.println("Argument one = "+args[0]); System.out.println("Argument two = "+args[1]); } }
Step 2) Save & Compile the code
Step 3) Run the code as java Demo apple orange.
Step 4) You must get an output as below.