Java First Program

In previous article we install java on our computer

In this article we write our first program and run it

There are two methods to run java program

  1.  Write code on notepad
  2. Using idle

Write code on notepad:

You have to open notepad on your pc

And write following code on it

// Your First Program
 class test {
   public static void main(String[] args) {
       System.out.println("Hello, World!"); 
   }
}
hello world program

Explanation of this program:

 Line 1: //your First Program

It is a comment in java. It is used to write the description. It is a non-executable part

Line 2:
Class test
{
// Body of class
}

Class is a keyword by which we define a class in java. Here test is a class name
{ } in this bracket we write code

Line 3:

   Public static void main(string arg[])
{
//Body of main method
}

 it is main method. java compile start executing code from the main method

  • public is an access specifier
  • Static is a keyword we will learn in a later article
  • void is a return type
  • main is the method name
  • (String args[])  it is the perimeter of the main method  

Main methods take an array  of string as an argument

Line 4:
System.out.pritnln(“helloWorld ”)

This is a print statement which prints anything written in this “ ” quotes
System is a class in java which has prlinln method


Now after writing code on a notepad :


Click on save as then save your file with . java  extension

Now run program:

Open command prompt:

Open the location where you save file

for example: Type “cd Desktop”

  • Type “javac <Filename.java>” example: test.java
  • Type “java <Classname>” example : test

Your program run successfully

2nd Method using ide:

To code in java, we use eclipses ide. In the previous article, we install java and eclipse ide.

Open eclipse idle:

Create new java project:

Create new class in myproject:

Right click on myproject —-> new  —-> choose class

Enter class name:

Write code and run:



SHARE THIS PAGE





Important Link