Program is a collection of 1) data(variables) 2) methods (operation).

  • What is the variable ?
    Ans- Variable is used to store data, such as a number, or a string of characters. Basically it is used to store some value(Data). Ex- int i = 5; So here i is a variable which store integer.
  • What is operation/method/function ?
    Ans- It’s a kind of process which will do some operation on variables and will give some output/result. Ex- addition of two numbers.
  • Variable follow three step process-
    i) Declaration (reserve some memory), ex- int i; 
    ii) Initialization (initialize the variable with some value), ex- i=0;
    iii) Utilization (use the variable)., ex- i+6;
  • Based on memory allocation java provide 8 types of data type-
    1. byte (1 byte storage),
    2. short (2 byte storage),
    3. int (4 byte storage),
    4. long (8 byte storage),
    5. float (4 byte storage),
    6. double (8 byte storage),
    7. char (2 byte storage),
    8. boolean (2 byte storage),
  • byte, short, int and long can store only integer variable. ex- 2
  • float and double used to store decimal number. ex- 3.0
  • char used to store a to z alphabet.
  • boolean can have true or false. It can’t have 0 or 1.
  • A program has keyword, identifiers and literals.
    ex- int i = 3;intis a keyword. 
    is a identifiers.
    is a literals.
  • Keyword is a word which is predefined.
  • Identifiers are words which are user defined. It can be alphanumeric but can’t be only numeric. 1st letter should always be alphabet.
  • Convention for identifiers- Always start 1st word with small letter but rest of the word should start with Caps letter.
    ex- javaSeleniumTraining
  • Convention for class name- all the word should start with caps letter.
    ex- JavaTraining
  • Note- If any local variable has not been initialized before using it then it will give compilation error.

 

  • Ex- 
    public class JavaTraining{
        public static void main(String[] args){
            System.out.println(“Start Here”);
            int x;
            x=4; 
            int y = 5;
            System.out.println(“value of x: “+x);
            System.out.println(“value of y: “+y);
        }
    }
  • + sign work as concatenation when using between String and integer or say to concatenate two values in java use + sign.
  • ; indicate the end of statement.

 

Ex-
public class Day{
    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.
  • Global 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.

Source: http://selenium-makeiteasy.blogspot.com