Thursday, 5 June 2014

Beginning with programming in JAVA

Getting Started With Basic Programs And Concepts :

The first very basic program to start with is :

  • TO PRINT HelloWord on the screen 
The coding initially can be done in notepad . For better understanding of programs , beginners are suggested to work on notepad and use command prompt for compilation and to run the program .

Coding to print the HelloWord is given as well as explained below :




Every word used in the above program has a specified meaning. Note that JAVA is a case sensetive language . In the above program some words have their first letter capital . All these concepts are explained below :

class
It is a reserved keyword used to create a class . This keyword is used as it is in the program . Every Program in java begins with the formation of class.

Hello
It is the name of class. First letter of the name of class should be capital . Not every time , but sometimes user can face problems in execution.

public static void main(String args[])
This whole statement works as void main()  in C++ . Note that execution of a program starts from this statement .  

public : It is a access specifier . There are three types of access specifiers . we will discuss the concept of access specifiers later . public here implies that any other program or method can access this class .

String args [] : It is a Command Line Argument (CLA) . S of String should be capital. It is a inbuilt class and note that in java whenever inbuilt class is used ... first letter is always kept capital. 

This whole statement is used to define the main program . The execution starts from here .

 System.out.println("HelloWord"); :

It works like cout command in C++ . To print the content on screen this command is used . System  is again an inbuilt class and hence S is kept capital. out  is a variable and println is a method . Functions in C++ are called methods in java . 

Note that there is difference between println and print command 
 In println command , the string or output is printed in next line . println command breaks the line and moves to next line whereas in simple print command , output is printed in the same line.

After typing this program in notepad , save the file with file name
                                                                 Hello.java
Note that the name of class and name of file should be exactly same. And the extension of file must be .java

Suppose that file is saved in followind address  :
                                                     C:\Users\HP\workspace

Then in command directory must be changed in following way :



Now to compile the program , the command given is    javac (Name of file).java
If there is no error in compilation process or program , cmd will ask for next command else a set of errors will appear on screen .
For error free program , we can run the prograam with the following instruction :  java (Name of file ) .

For  clarification refer the image :




NOTE : 
Before starting this whole process make sure that valid version of JAVA is installed on your system and also for execution of programs on cmd user must the change the PATH .

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | coupon codes