Showing posts with label Java Training. Show all posts
Showing posts with label Java Training. Show all posts

Saturday, November 22, 2014

Day9 (Java Tutorial)...topis- method overloading.

Please continue from here-
http://selenium-makeiteasy.blogspot.in/2014/11/day8-java-tutorialtopis-this-keyword.html

*Method Overloading
        developing the same method with different argument list is called as method overloading. The argument list should differ in either of below 3 ways-
  • Type of arguments should be different.
  • Number of argument should be different.
  • Position of arg should be different.
ex-
 class="prettyprint prettyprinted"
public class Demo {
    public static void main(String[] args) {
        System.out.println("program starts");
        add(5, 10);//this will call add(int a, int b)
        add(4.6, 10); //this will call add(double a, int b)
        System.out.println("Program ends.");
    }
    public static void add(int a, int b){
        System.out.println("add(int a, int b)");
    }
    public static void add(double a, int b){
        System.out.println("add(double a, int b)");
    }
}

output
---------
program starts
add(int a, int b)
add(double a, int b)
Program ends.

Note-


  •  Inside a class, both static method as well as non-static method can be overloaded.
  • In java, methods can not be overloaded by giving different return type.
  • java identify the overloaded method based on the arguments, it does not identify based on the return type nor 'static' keyword.
  • There can't be two methods with the same method signature inside a class.

Day8 (Java Tutorial)...topis- this keyword, this() operator

Please continue after from here-
http://selenium-makeiteasy.blogspot.in/2014/11/day7-java-tutorialtopis-constructors.html

*How to initialize or refer non-static members or instance members without creating object ?

Ans-
by using 'this' keyword.

*The Keyword 'this' is used to refer the current class members (global variable and methods). 'this' keyword can be used in constructor as well as non-static methods.

ex-
public class Demo {
    int a;
    Demo(int a){
        this.a = a;
    }
    public static void main(String[] args) {
        System.out.println("program starts");
        Demo ref = new Demo(10);
        System.out.println("value of a-> "+ref.a);
       
        System.out.println("Program ends.");
    }
}

output
---------
program starts
value of a-> 10
Program ends.



this() operator-
-------------------
  • A constructor of a class can invoke another constructor of the same class using 'this()' operator.
  • Recursive constructor invocation is not possible using "this()" operator.
  • Inside a constructor "this()" statement should be always the 1st statement inside the constructor body.
 ex-

public class Demo {
    Demo(){
        this(10);//this statement should always be 1st statement inside constructor.
        //this will call the Demo(int a) constructor
        System.out.println("default constructor.");
    }
    Demo(int a){
        System.out.println("this is called from default constructor.");
    }
    public static void main(String[] args) {
        System.out.println("program starts");
        Demo ref = new Demo();//this will call default constructor
        System.out.println("Program ends.");
    }
} 

output
 program starts
this is called from default constructor.
default constructor.
Program ends.

Day7 (Java Tutorial)...topis- Constructors, and basic concept of java file.

please continue from here-
http://selenium-makeiteasy.blogspot.in/2014/11/day6-java-tutorialtopis-explanation-abt.html

How to save a java file-

         A java file/program can have number of classes and we can save the file with any of the class name. The compilation will go fine and compiler will create the class file for all the classes in the program.
         Now one can run any of the class. The classes which have main method, only those will be executed successfully, if main method not found then will give error(main method not found).
         This is the reason why we give compilation command as
Javac fileName.java
and run command as
java className


ex- here class A can not be run, only class Demo can be executed.
 
class A{
    int a = 10;
}
public class Demo {
    public static void main(String[] args) {
        System.out.println("program starts");
        System.out.println("Program ends.");
    }
}


Note-
  • For each separate class in a java file/program, separate static pool will be created that is each class will have its own static pool.
  • One can write any number of classes in a java file, with any number of java definition block having i.e. that is every class can have its own main method.
  • If any class is declared as public, then file will have to save with the public class name only. 
  • There can not be more than one public class in a file/program.
===================================================
Constructor
---------------- 
  1. Constructor is special type of method which is executed at the time of instance or object creation.
  2. In java, every class should have a constructor, if a class does not have any constructor then compiler writes a constructor at the time of compilation such constructors are called default constructors.
  3. Default constructor will not have any argument.
  4. A programmer can also develop the constructor inside the class, in such cases programmer has to follow below rules-
    •   Constructor should not have any return type not return value             declared.
    •  Constructor name should be same as class name.
  5. When object is created by 'new' operator, the constructor has to be called by the 'new' operator.
  6. While creating an instance of a class if one have to perform any task at the time of instance creation, declare that particular task inside the constructor body.
  7. Constructor are used to initialize instance member (non-static members) of the class at the time of object creation.
  8. In a single java class, we can develop more than one constructor, the argument list should differ b/n the constructors such constructors are called overloaded constructors. The argument list should differ in either of the below 3 ways-
    • Type of arguments should be different.
    • Number of argument should be different.
    • Position of arg should be different.
  9.  When one have to create an instance of a class with different initialization or different operation at the time of instance creation, then we go for overloaded constructors.
  10. When java is invoking the constructors, constructors are invoked based on the argument list.
Note-
  • There can not be any java program without constructor.
  • instance and object both are same. 
 Syntax to create constructor- (Here methodName will be same as className.)

 methodName(args/no args){
       ............................
       constructor body
       ............................
}

Points to be remember-
  • As soon as object is created, it will call the constructor.
  • If one do not have object then constructor will not be called.
  • If one don't write any constructor then compiler will write the default constructor while compilation.
  • If a programmer write even a single constructor, compiler will not write any constructor.
  • Default constructor has the same access modifier as the class.
ex-
1)
public class Demo {
    Demo1(){
        System.out.println("This is constructor which is called by new operator while object creation");
    }
    public static void main(String[] args) {
        System.out.println("program starts");
        Demo object = new Demo();
        System.out.println("Program ends.");
    }
}


Output-program starts
This is constructor which is called by new operator while object creation
Program ends.

2)
public class Demo2 {
    Demo2(){
        System.out.println("This is constructor which is called by new operator while object creation");
    }
Demo2(int a){
        System.out.println("This is constructor which is called by new operator while object creation in which int arg passed. Value of arg: "+a);

    }
    public static void main(String[] args) {
        System.out.println("program starts");
        Demo object1 = new Demo();
        Demo object2 = new Demo(10);
        System.out.println("Program ends.");
    }
}

Output-program starts
This is constructor which is called by new operator while object creation
This is constructor which is called by new operator while object creation in which int arg passed. Value of arg: 10
Program ends.



Day6 (Java Tutorial)...topis- explanation abt class execution, some imp points abt java.

Please continue from here-
http://selenium-makeiteasy.blogspot.in/2014/11/day5-java-tutorialtopis-static-non.html

Explanation of Java class execution-

----------------------------------------------
When java class is executed, the following steps will happen-
  1. Memory gets allocated for the execution.
  2. The allocated memory is divided into stack area and heap area.
  3. stack is an implementation of first in and 1st out, which is used for execution purpose.
  4. Heap area is a random storage area which is used to store the information of program.
  5. Java or JVM enters the stack area for its execution.
  6. Java calls class loader program to load all the members of class.
  7. The class loader program loads only the static members of the program into a specific area on the heap called Static Pool.
  8. Now Java calls main method to stack area for its execution, the main method starts executing each statement one by one.
  9. When java or jvm comes across instance or object creation statement, the object is created in the heap area and object address is stored in the reference variable.
  10. At the time of object/instance creation, all the non-static members of the class are loaded into object and address of that object is saved into reference variable.
  11. Java before exiting the stack area, it calls Garbage collector which frees cleans the memory in the heap area.
Note-
  • In entire program, only one copy of the static member exist.
  • Multiple copies of non-static members can exist, which depends on number of times the object is created.
  • Static members are loaded at the time of class loading.
  • non-static members are loaded at the time of instance(Object) creation.
  • Static members(method or var) can be directly access in static method as well as non-static method.
  • Non-static members of the class can be directly access only in non-static methods.
  • To access non-static members, inside static method, an object of the class has to be created. Then use the reference variable to access non-static members.
  • An Object whose ref address is lost know as Abundant Object. To free the memory of abundant object before program termination, the programmer has to call the garbage collector explicitly.
Example-
public class Demo {
    int nonStaticVar=10; //this needs to called by ref var
    static String staticVar = "static variable, this can be called directly";
    public static void main(String[] args) {
        System.out.println("program starts");
        Demo refVarName = new Demo();  //this is creating the object
       
        System.out.println(refVarName.nonStaticVar);
        System.out.println(staticVar);
       
        refVarName.nonStaticMethod();
        staticMethod();
       
        System.out.println("Program ends.");
    }
    public static void staticMethod(){
        System.out.println("static method, this can be called directly.");
    }
    public void nonStaticMethod(){
        System.out.println("non-static method, this need to call by ref var");
    }
}

output
program starts
10
static variable, this can be called directly
non-static method, this need to call by ref var
static method, this can be called directly.
Program ends.

===================================================
very basics information (Though it's very basic but trust me people not able to ans when asked in interview :P ;))-

Arithmetic Operators-

===============


Operator          Result                   ----------------            -----------
+                        Addition
-                          Subtraction
*                         Multiplication
/                         Division
%                       Modulus
++                     Increment
--                        Decrement
+=                     Addition Assignment
-=                       Subtraction Assignment
*=                      Multiplication Assignment
/=                       division Assignment
%=                     modulus Assignment.

Relational Operators-
===============
Operator          Result                   ----------------            -----------
==                     Equal to
!=                      Not equal to
>                       Greater than
<                       Less than
>=                    Greater than or equal to
<=                    less than or equal to


AND and OR condtions-
------------------------------

syntax for AND condition-
---------------------------------
 &
&& (this is short and)

explanation -
if (x>0 && y>0)    ---> in this case here if x<= 0 then it will not go to check for the other condition y>0.

but if (x>0 & y>0) ---> in this case even if x<=0 then also it will go to check for the other condition y>0.

syntax for OR condition-
---------------------------------
|
|| (this is short or)

explanation -
if (x>0 || y>0)    ---> in this case here if x>0 then it will not go to check for the other condition y>0.

but if (x>0 | y>0) ---> in this case even if x>0 then also it will go to check for the other condition y>0.

===================================================



Friday, November 21, 2014

Day5 (Java Tutorial)...topis- static, non-static members, object and class

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

*Actual way of calling static members (variables and methods) is

className.staticMemberName.

ex-
1)
public class StaticMembers {
    public static void main(String[] args) {
        System.out.println("## program starts ##");
        System.out.println("Addition of two numbers "+StaticMembers.add());
        System.out.println("## program ends ##");
    }
    public static int add(){
        int a = 5;
        int b = 6;
        int c = a+b;
        return c;
    }
}


output-
## program starts ##
Addition of two numbers 11
## program ends ##



2)
public class StaticMembers {
    static double a = 34.5;
    public static void main(String[] args) {
        double a = 43.6;
        System.out.println("program starts");
        System.out.println("local variable a- "+a);
        System.out.println("global variable a- "+StaticMembers.a);
        StaticMembers.staticMethod();
        System.out.println("Program ends.");
    }
    public static void staticMethod(){
        System.out.println("static method.");
    }
}


output-
program starts
local variable a- 43.6
global variable a- 34.5
static method.
Program ends.


==========================================================================
**Convention for method name-
            *1st letter should be small of 1st word then the 1st letter of the rest of the word should be caps.


Assignment-
1) write a program to calculate simple interest.

2) write a program to calculate volume of sphere.

*Final Variables- variables which needs to be initialize at the time of declaration and which can not be reinitialize.

ex- final static double pi = 3.14;

Ex-
public class Circle {
    final static double pi = 3.14;
    public static void main(String[] args) {
        System.out.println("program starts");
       
Circle.areaOfCircle(2);
        System.out.println("Program ends.");
    }
    public static void areaOfCircle(double r){
        double area = pi*r*r;
        System.out.println("area of cicle -> "+area);
    }
}


Note- 
  • Final means no more changes. 
  • Local variables can also be final variables.
  • Final variables must be initialized else it will give compilation error.
==========================================================================
**Non Static Members (variables and methods)
       If a global variable or method is not declared as static then that variable or method is non-static.

*How to create an Object-
        ClassName     refVarName     =     new     ClassName();

*How to call non-static members
       Create an object and then use the reference variable to call the non-static member.

      
         ClassName     refVarName     =     new     ClassName();
         refVarName.nonStaticMemberName;

ex- 
public class NonStaticMembers {
    int nonStaticVar;
    public static void main(String[] args) {
        System.out.println("program starts");
      
        NonStaticMembers refVarName = new NonStaticMembers();  //this is creating the object
        System.out.println(refVarName.nonStaticVar); //this is calling non-static variable , this will print 0 because
        //if global variable is not initialize then compiler will by default initialize it with default value
        refVarName.nonStaticMethod();  //this is how to call the non-static method
        System.out.println("Program ends.");
    }
    public void nonStaticMethod(){
        System.out.println("Inside non-static method.");
    }
}
 

output-program starts
0
Inside non-static method.
Program ends.


===================================================
Class and Object- 
     Class is a template.
     Object is a mirror image of the class. We can create 'n' number of the objects.

ex-

 public class NonStaticMembers {
    int nonStaticVar;
    public static void main(String[] args) {
        System.out.println("program starts");
       
        NonStaticMembers refVarName1 = new NonStaticMembers();  //this is creating the object1
        NonStaticMembers refVarName2 = new NonStaticMembers();  //this is creating the object2
        System.out.println(refVarName1.nonStaticVar);
        System.out.println(refVarName2.nonStaticVar);
        System.out.println("Program ends.");
    }
}

 output-
program starts
0
0
Program ends.





            

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.

Wednesday, October 15, 2014

Day3 (Java Training)

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

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;

    int
    is a keyword.
    i is a identifiers.
    3 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.
That's all for today :)

For your practice-
1) Try to write the code to print the sum of two numbers.
2) Try to write the code to print the area of square whose one arm is 5cm.

Sunday, October 12, 2014

Day2(Java Training).

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

Installation of JDK-
  • go to Google - download latest version of JDK and install it.
    http://www.oracle.com/technetwork/java/javase/downloads/index.html
  • After installation, go to -> my computer -> C drive -> program files(x86)/program files -> search for Java folder and open -> jdk -> bin -> copy the path of bin.
  • open the computer properties -> Advanced System settings -> Advanced -> Environment variable -> under system variable search for Path -> click on edit -> go to end of line -> insert ; -> then paste the path of bin which you copied in the above step -> 3 times click OK button.
  • JDK installed.
  • To verify whether JDK installed or not - go to cmd and type the commad -> Java -version and hit enter. This will give you the java version which you have installed.

Every program should have 3 things-

  • keyword (all keyword should be in lower case.)
  • identifiers
  • literals
 Important points-
  • *Every java module should start with 'class' keyword.
  • While saving the program, class name and file name should always be same.
  • Space is not allowed in class name.
  • If there are more than one class declared inside a program, then always save the file with class name which has access specifier as public. (This concept will clear in next topics so don't worry.)
  • Compiler recognize the java program with the extension .java
  • Ex-
    class HelloProgram
    {
               public static void main(String[] args){
                     System.out.println("Hello Java");
               }
         }
  • Save the above program as HelloProgram.java
  • Convention- give the 1st letter as caps in class name for all the words used in class name.
  • Explanation-
    public, static, void are keywords.
    main() is method
    String is a class
    args is the argument
  • output of this program is Hello Java
How to compile and run java program ?
  • go to cmd.
  • go to that folder where java file is kept.
  • To compile java program command is -
    javac filename.java
  • To run/execute the class file command is -
    java ClassName
Note- 
  •  All java program execution start from main method.
    Syntax for main method- public static void main(String[] args)
  • Every statement in java program should end with semicolon(;).
Points to remember for interview
  • Can you compile and run the java program without main method ?
    Ans- Java program can be compiled without main method,
             but can not be run/executed without main method.
  • What is the difference between println and print ?
    Ans-
    println is used to print in the next line while print is used to print in the same line. Basically println will move the cursor to next line while print will remain the cursor in the same line.
    ex- System.out.println("Java");
          System.out.print("And");
          System.out.println("Selenium");
    output- Java
                And Selenium
  • Byte code is called as class file.
  • Whether java is compiler based or interpreter based ?
    Ans- java required both compiler and interpreter.

Day1 (Java Training)

Why selenium ?
  • It can be used for many OS (almost all the OS).
  • Supports many programming language. Ex- Java, Python, C# and Ruby etc.
  • It's an open source as well as very effective.
  • Test script execution is much faster than QTP.
  • Easy to learn and understand.
What is the difference between Scripting and Programming language ?
  • By using programming language, we can develop application.
  • Scripting language is used for testing/validating application. Using scripting, application can not be developed.
What is a Program ?
  • Program is a set of instruction.

Here, we are going to learn selenium using core-Java.



Interpreter- One line at a time and convert it into LLL.
Compiler- It take entire program in one shot.

JVM- Java virtual Machine- It's work as an interpreter.
JRE- Java run time environment.

Why Java is so popular or Best feature of Java ?
  •  Java is platform independent.
    Explanation- Suppose if we have executed byte code, then JRE is sufficient to run that. (as explained in above diagram).
                 To make platform independent convert the java code into byte code. And as we can execute this byte code on any OS that's why Java is called as platform independent.

Topic's need to cover for Core Java-
  • Basic idea of programming language. (done above),
  • Class, Members of Class,
  • Method overloading,
  • Constructor,
  • Method overriding
  • Type casting
  • Interface
  • Encapsulation
  • Polymorphism
  • Inheritance
  • Array class
  • Object class
  • String class
  • Exception Handling
  • Wrapper class, 
  • Collection
  • File handling
  • Threads
  • Rest if anything missing above


That's all for today... :)