Tuesday, August 5, 2008

Java For Beginners

What is Java?
1.It is Object Oriented Programming Language.
2.It is platform independent.
3.It has automatic memory management.


What is Object Oriented Programming?
It has 5 properties.
1.Abstraction
2.Encapsulation
3.Inheritance
4.Modularity
5.Polymorphism


What are the things you need to run a java program?
1.JDK software (available in http://java.sun.com/javase/downloads/index_jdk5.jsp)
2.Set the environmental variables


How to set the environmental variables
There are two methods.
First Method:
1.Right click on My Computer,and select properties.
2.Select the Advanced tab.
3.Click on Environmental variables button on the bottom.
4.In the System Variables, click on New button.
5.Set Variable Name : CLASSPATH
Variable Value :C:\jdk1.5\lib; and click OK. 6.Again click New Button. 7.Set Variable Name : PATH Variable Value :C:\jdk1.5\bin; and click OK.

Second Method:
1.Goto command prompt and navigate to the corresponding folder.
2.set path=c:\jdk1.5\bin;
3.set classpath=c:\jdk1.5\lib;.;


How to write a java program?
1.Open Notepad
2.Write the program
3.Save it with .java extension


Important things to be noted while writing a Java Program
1.Java is case sensitive.
2.Be careful with the filenames,it should match with the class names(case).
3.There should semicolon after every statement.


General Structure of a Java program
public class CLASSNAME
{
 public void METHODNAME(){
    //method body
 }
 public static void main(String args[]){
    //body
 }
}


Hello World
public class HelloWorld{
 public static void main(String args[]){
    System.out.println("Hello World");
 }
}
c:\>java HelloWorld
Hello World


Command Line Arguments
public class CmdArgs{   public static void main(String args[]){     System.out.println("The first argument is " + args[0]);   } }
c:\>java CmdArgs Hello
Hello

Usage of If-else
public class ExIf{   public static void main(String args[]){     if(args[0].equals("args")){       System.out.println("You entered args");     }     else{       System.out.println("You did not enter args");     }   } }
c:\>java ExIf args
You entered args

Usage of For loop
public class ExFor{   public static void main(String args[]){     for(int i=1;i<=args.length;i++){       System.out.println("The " + i + "th argument is " + args[i-1]);     }   } }
c:\>java ExFor One Two Three
The 1th argument is One
The 2th argument is Two
The 3th argument is Three

Usage of while loop
public class ExWhile{   public static void main(String args[]){     int i=1;     while(i<=args.length){       System.out.println("The " + i + "th argument is " + args[i-1]);       i++;     }   } }
c:\>java ExWhile One Two Three
The 1th argument is One
The 2th argument is Two
The 3th argument is Three

No comments: