"Hello World" Java program and How it executes?

First  Program of Java

After successful  Installation of java (jdk) , Now we got the environment where we execute the java program.

Now move towards first java program.


Sample Program :

class Hello
{
    public static void main (String ar[])
{
     System.out.println("Hello World");
}
}

Lets understand the program :
"class","public","static","void" all are Keywords.

Note: Keywords always starts from small letter.

"Hello", "String","System" all are classes.

Note : If we follow the programming standards class always start with capital letter.

"main()","print()" are the method because it  suffix contains parenthesis "( )".

Note: methods use camel case for example getName(). First words start with small letter and second words start with Capital latter.

"Hello world" is a message or String because it is written in double quotes (" ").

Now we we understand the Line by Line of Sample Program


Line 1 :  class Hello  - Here we have create a class by using "class" keyword with  class name "Hello".

Line 2: {  - here starting curly bracket (braces) tells, our "Hello" class is started
.
Line 3:  public static void main (String ar[]) -  public static  void main( String ar[]) is heart of java program  Because java program start execution from main() method. If a class not contain main() method  we can not execute the program. we will get error at run time. Because  public static void main (String ar[]) is configured with JVM . JVM search main() method for execution.

Now question is why main() method is public static void main (String ar[])?

public -  main() method can be access from anywhere , here public ia an access specifier which tells you can access this method from anywhere.

static -  No object is required to invoke main() method. It  means In Java for calling the methods we need to call methods by object with dot operator.
But in java main() method define as static so that no need to call main method by object.

void - The return type of this method is void means there is no return value.

main(): Name of the main function that gets executed. It is the Program Life-Cycle.

String ar[]  - This array is the mechanism through which the runtime system pass information to your program. For example:
           java Hello arg1 arg2
 Each String in the array is called a command-line argument.
Line 4 : {  - This starting curly brackets (braces) {  tells, our main() method is started.

Line 5 :  System.out.println("Hello World")

System :this is a build-in class. given by java corporation. this shows our all machine operations. In general way we people also called system to our computer. This class has all computer related functions.
out: this refers to "output device". it is called as "System.out" means it will call the System's output device i.e. Monitor.
println("Hello World"): here println() is the method which help to print given Message into the monitor.
In simple word, u r printing "Hello World" message into monitor (output device) of System (Computer). Now u can understand the meaning of whole "System.out.println("Hello World")" line.

Line 6:   } - Closing the main() method.
Line 7:   } - Closing the class Hello.


Some Basic steps to write java code and execute  by using Notepad:
1. Open Notepad
2. Type the Program:
Please write your program very carefully. Java is the highly case sensitive language.If you need to write all words in right case.
3. Save It:
If you have set the path of jdk  in Environment variable then you can save anywhere in your computer. Otherwise, Save it in bin directory of JDK [C:\Program Files\ Java\ Jdk..\Bin \ Hello.java ]
Class Name and File Name should be same.
Extension should be ".java"
It is Case Sensitive.
4. Compile It:
a) If  jdk path is set in Environment Variable:
In cmd  change the directory where your program is save.
b) If  jdk path is not set in Environment Variable:
First you need to copy path of our "bin" directory of JDK folder [C:\Program Files\ Java\ Jdk..\Bin ]
Go to Command Prompt:
cd paste_your_coiped_path
like,
cd C:\Program Files\ Java\ Jdk\Bin
You will enter in given "bin" path.
now,
javac Hello.java
this above line we are using to call the java compiler. here "javac" is our java compiler. and compiler will compile the Hello.java file. and create the byte code. i.e. "Hello.class" file in same directory.
5. Interpret It:
in same command prompt write:
java Hello
this above line we are using to call interpreter to execute the Hello.class file.
now You will get your output in command prompt.

Some Important Points:
1. A Source file is created with extension at .java (dot java).
2. The main() method must be declared inside class only.
3. The file name of the Source file must exactly match with the class name, inside which main() methods is declared.





SHARE

About prabhakar jha

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment