pTutorial

New way to learn Programming

First Java Program

Java

In this document we have discussed about the hello program of java or you can say how to write first console based java program.

To create Java program, you must have install some software like JDK (Java Development Kit ).

Requirement for creating console based Java program

  • You have to install JDK (Java Development Kit ) Click here to Download Current version of JDK.
  • Text Editor like Notepad –Its comes under the every window so its available for you.
  • Set the path of the jdk/ bin directory that comes after the installation of JDK.
  • Type your first Program.

Steps of creating First Java Program

1. Open Notepad or any text editor, To open Notepad start menu -> All Programs -> Accessories -> notepad or press window button and search.

2. Type the above code on notepad and saved it as any name, in my case its JavaExample.java.

Simple Java Program

//JavaExample.java
class JavaExample{  
    public static void main(String p[]){  
     System.out.println("Welcome to the java word");  
    }  
}  

How to compile Java program

Open CMD (Command prompt), to open CMD press Windows + R and type cmd and press enter. Go to the directory where Java program is saved. Compile and run

Compile : javac JavaExample.java 
Run : java JavaExample

Note : Java is a case sensitive language so write the code same as above.

Save this java program with the name "JavaExample.java"

Output

Welcome to the java word

Explanation of First java program

Let’s see the explanation of the each and every word that used in first java program one by one.

  • First line of this file represent the comment of this program.
  • Class is the keyword that is basically used to declare a class.
  • JavaExample is the name of class
  • Public is also a keyword that represent that the class is visible for all.
  • static is a keyword
  • void is the return type of the java program, it means that it will not return anything.
  • main is starting point for execution in every Java program.
  • String args[] is a String type array used for command line argument.
  • System.out.println is used to print anything on console. We will learn in detail in later sections.

Note: It's mandatory to write every statement within a class block.


Importance of main method

Java main method is the start point of the console based application or you can say standalone application in java. Java program runs until the main method finishes.

main method signature

There are some valid and invalid signature of method are following below-

Valid java main method signature

public static void main(String []args)
public static void main(String[] args)    
public static void main(String... s)  
public static void main(String s[])  
static public void main(String[] s)  
final strictfp public static void main(String[] s)  
public static final void main(String[] s)  
final public static void main(String[] s)  

Invalid java main method signature


    public void main(String[] s)  
    static void main(String[] s)  
    public void static main(String[] s)  
    abstract public static void main(String[] s)  

It’s not mandatory that to write args in every time, It’s just array variable so it could be any variable name.


QUOTES OF THE DAY

Tomorrow life is too late, live today.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER