Saturday, 23 November 2019

SE - 42 - Major interfaces used in Selenium

Lets see few of the interfaces which are used in Selenium. The list also contains few of the JAVA interfaces which may be used in terms of Selenium :

1) WebDriver
2) WebElement
3) Alert
4) TakesScreenshoot
5) All the Listners (Eg : WebdriverEventListener , ItestListener ,etc)
6) SearchContext
7) Iterable , Collection,List,Queue, Set , etc
8) Wait
9) Comparator
10)JavaScriptExecutor
11) Row , Cell (Apache POI)

SE - 41 - Island of isolation in JAVA

Island of isolation is a term given to a condition where 2 objects are ready for garbage collection when the following conditions are met :

1) Object 1 references to Object 2
2) Object 2 references to Object 1

      Test i;
      public static void main(String[] args) 
      {
          Test t1 = new Test();
          Test t2 = new Test();
          t1.i = t2;
          t2.i = t1;
          t1 = null;
          t2 = null;
          System.gc();
      }

SE - 13 - Static and non-Static Methods !!

- We can call static methods directly while we can not call non static methods directly. You need to create and instantiate an object of class for calling non static methods.

- Non static stuff (methods, variables) can not be accessible Inside static methods Means we can access only static stuff Inside static methods. Opposite to It, Non static method do not have any such restrictions. We can access static and non static both kind of stuffs Inside non static methods

- Static method Is associated with the class while non static method Is associated with an object.

- Static methods can't be overrided in java. If we try to override a static method Java treats it as a new method and rule of parent/child doesn't applies to it

-Static methods can be overloaded in JAVA with rules of overloading

Monday, 18 November 2019

SE - 16 - Caching in Java for Integers

In java "==" is used to compare object references. The caching in Java allows to reuse the referenced objects and save memory. So in the given range of caching the same object reference is used and the comparison results as true while beyond the range it returns false.

Note :
1) Caching was introduced in Java 5
2) It basically improves the performance
3) Caching range lies between -127 to +127
4) Caching applies for Integer , Byte , short , Long and Character objects too
5) It only works for autoboxed type of objects. It is not applicable for normal object declaration(eg : Integer integer1 = new Integer(123);.


Example :
 public static void main(String[] args) {
  Integer integer1 = 123;
  Integer integer2 = 123;
  
  Integer integer3 = 128;
  Integer integer4 = 128;
  
  if (integer1 == integer2)
   System.out.println("integer1 == integer2");
  else
   System.out.println("integer1 != integer2");
  
  if (integer3 == integer4)
   System.out.println("integer3 == integer4");
  else
   System.out.println("integer3 != integer4");
  
 }


Output:
integer1 == integer2
integer3 != integer4





SE - 15 - String methods in Java !

1) To compare two strings
 - str1.compareTo(str2) [3-way comparison (positive ,negative,0)]
 - str1.equals(str2) [2-way comparison(true,false)]

2) To join 2 strings
 - str1.concat(str2) [Its a method]
 - str1 + str2 [Its a operator]

3) to find the character by index position
 - str1.charAt(1)

4) to compare 2 strings ignoring the case
 - str1.equalsIgnoreCase(str2);

5) to convert a string to upper or lower case
 - Str1.toUppercase();
 - Str1.toLowercase();

6) to remove spaces from both sides of string
 - str1.trim();

7) to trim a part of string
 - Str1.substring(2,12); [based on index]

8) It checks that a string ends\starts with specified suffix and it result boolean result
 - str.endsWith("ABC") [checks if the string ends with ABC]
 - str.startsWith("ABC") [checks if the string starts with ABC]

9) to find the length of a string
- str.length();

10) to replace any word with given word in a string
 - str1.replace("AB","CD")


SE - 40 - Copy Constructor in Java

We can create a copy constructor by giving the parameter as object of same class . Once created then we copy each field  of the input object into the new instance

Note :
1) Java never creates a copy constructor by default rather we need to manually create it.
2) Copy constructor and cloning in Java are not same

public class Student {
    private int roll;
    private String name;
     
    public Employee(Student stu) {
        this.id = stu.roll;
        this.name = stu.name;
    }
}



SE - 39 - Some basics about thread in Java

Lets see some of the basics about thread in java :

1) Thread can't be started twice. It can be started only once by using start() method. If we attempt to start the thread twice we get IllegalThreadStateException.

2) We can create Thread in Java in 2 ways :
     a) By implementation of Runnable class
     b) By extending Thread class

3) Only start() method is used to start the thread. If we call the run() method then the Thread will not be called rather it will behave as normal method calling

4)  Suppose if there is any statement written (Eg : printing some text , etc) after start() method in thread then in output we will see the thread initiated after the execution of next statement.  Java will execute the next statement then start the thread.

Sunday, 10 November 2019

SE - 7 - Methods and constructors calling in Inheritance

In inheritance the Methods are called in sequence of Bottom to Top(Child to Base) and Constructors are called from Top to Bottom (Base to Child). Lets see through an example.

Lets assume there are 2 classes , Base and Child with one constructor each and one common(overridden) method in each of them.


public class ABBA 
 { 
     public static void main(String[] args) 
     { 
         Base x = new Base(); 
         Base y = new Child(); 
         Child z = new Child(); 
         System.out.println("Object of base class");
         x.Print();
         System.out.println("Object of child class by giving reference of base class");
         y.Print();
         System.out.println("Object of Child class");
         z.Print();
     } 
 } 
 class Base { 
   Base(){
    System.out.println("Base Constructor"); 
   }
      public void Print() { 
          System.out.println("Base Method"); 
      }          
  } 
    
  class Child extends Base { 
   Child(){
    System.out.println("Child Constructor"); 
   }
      public void Print() { 
          System.out.println("Child Method"); 
      } 
  } 

Output : 

Base Constructor
Base Constructor
child Constructor
Base Constructor
child Constructor
Object of base class
Base Method
Object of child class by giving reference of base class
Child Method
Object of Child class
Child Method

Lets understand the program. So when there is a base and child , there can be 3 possibilities of object creation : 

1) Creating object of child class
2) Creating object of base class
3) Creating object of child class by giving reference of base class.


What will happen : 
1)  In all the 3 scenarios the base constructor will be called for sure since we have inherited base class and constructor is called from Top to Bottom , that is from base to child.

2) When we will create object of base class then only the Base constructor is called. The child constructor is never called.

3) When we create object of child class in scenario 1 and 3 above the base and child constructors will be called.

4) When object of base class is called by giving reference of base class then only method of base class will be called.

5) In case of child class object(scenario 1 and 3), if the method is called which is overridden by child class then only child class method is called. Whenever we create object of child class then only the overridden methods are called. Method calling works from bottom to top.

6) If we call a method which is not present in child class then base class method will be called.

7) If a method is not present in base class and present in child class , then it will give error when we try to access it in scenario 3 (Creating object of child class by giving reference of base class)

8) In scenario 1 and 3 , only the overridden method will be called. Base class method which are overrided will never be called.

SE - 38 - Static Initialisation block.Vs Instance Initialisation block vs Constructor

The order in which the Static Initialisation block ,  Instance Initialisation block and Constructors are called is as below:

1) Static Initialisation block - This is called when memory is allocated , so this one is called very first. If there are multiple blocks then it is called in the order they are written.

2) Instance Initialisation block - This is called before the Constructors.If there are multiple blocks then it is called in the order they are written.

3) Constructors : Constructors are called at last.

Lets quote an example to clearly understand difference between these 2 type of blocks:

public class Test 
 { 
     public static void main(String[] args) 
     { 
         A a = new A(); 
     } 
 } 
   
class A 
{ 
  
//first static initialization block
static {
 System.out.println("Static-1");
}

//first Instance Initialization bloc
{
 System.out.println("Init-1");
}

//Constructor
A(){
 System.out.println("constructor");
}

//second static initialization block
static {
 System.out.println("Static-2");
}

//second Instance Initialization bloc
{
 System.out.println("Init-2");
}} 


Output : 
Static-1
Static-2
Init-1
Init-2
constructor

So here we can see that Static blocks are called first in the order they were written , then instance blocks were called and at last constructors were called.