Friday, October 17, 2014

Day4 (Java Training)

Please continue from here-
http://selenium-makeiteasy.blogspot.in/2014/10/day3-java-training.html

Ex-
public class Day4{
    public static void main(String[] args){
        System.out.println("Start Here");
        System.out.println("This will print");
        //System.out.println("This will not print");
        /*System.out.println("This will not print");
        System.out.println("this will also not print");*/
        System.out.println("end here");
    }
}


  • How to comment the statement or set of statements in java ?
    Ans- //  -> double forward slash is used to comment a single statement.
             /*   */  -> is used to comment set of lines. Please refer above example.
             /*
                 statement1;
                 statement2;
                 ..................
            */
  • Types of methods and variable ->
    > A program contains variables and method.
  • Program
    1. Variable
       i) Local variable
       ii) Global variable
            a) static global variable
            b) non-static global variable
    2. Methods
      i) static methods
      ii) non-static methods
  • Local variable- Variable declared inside a method is know as local variable. It can be accessed in that method where it has been declared. Every local variable has to be initialized before accessing it. If it has not been initialized before accessing it then program will give compilation error.
  • Gloable variable- Variables declared inside the class but outside the method are known as global variables. It can be accessed in any method. Global variable need not to be initialized. Compiler will initialize with the default value.
  • ex-
    class ABC{
           int a;   // global variable
           public static void main(String[] args){
                  int b=4; // local variable
                  System.out.println(a); //this will print 0 which is by default initialize by compiler
                  System.out.println(b); //this will print 4
           }
    }
  • If local variable and global variable have same variable name then preference will be local variable.
  • Syntax to declare static method-
    static return_type method_Name(args/no args){
        statement------
        return value;
    }

    ex- static void multiple(){
                int a = 2;
                int b = 4;
                System.out.println(a*b);
         }
  • If don't want to return any value then don't use return value, as shown in above example. And use the return_type as void.
Note- Java execute only those statement which are inside the main method.

Example-

class Day4{
      public static void main(String[] args){
                System.out.println("Program starts here.");
                addition();  //this is calling the addition() method
                System.out.println("Program ends here.");
      }
      static void addition(){
                int a = 4;
                int b = 5;
                System.out.println(a+b);
      }
}

output-
 Program starts here.
9
Program ends here.

No comments:

Post a Comment