Java
Java
Class Test
{
private static Test t = new Test ();
private Test () ---If it is not private any person creates more objects
{
}
public static Test getTest ()
{
return t;
}
}
2)Lazy Initialization: creation of object when required (Runtime creation)
Class Test
{
private static Test t = null;
private Test () --If it is not private any person creates more objects
{
}
private static test getTest ()
{
if(t==null)
{
t = new Test ();
}
return t;
}
}
Double Dimensional Array: it stores the values in the form of row and column.
int a [][] = new int [][];
7.What is String?
A string is a collection of characters which comes from java. lang. string package and it is
immutable because immutable cannot be changed and another reason why string is immutable
could be it is used in HashMap technique as key.
if (str == str1)
{
}
e.g.: String str = new String("hello");
String str1 = new String("hello");
if (St. Equals(str1))
{
}
String Methods:
=============
equals()
equalsIgnoreCase()
contains()
charAt()
toUpperCase()
toLowerCase()
replace(old,new)
trim()
IsEmpty()
endsWith()
Example:
int x=30; // primitive datatype
Integer y=x; //Wrapper class
The reason java is not 100% oop language its 99% only because primitive datatypes are not
object oriented which derived from C language to make it 100% oop language we will use
wrapper classes.
We will use in Collection concept as well
POLYMORPHISM: One task can be performed in different ways and a polymorphism can be
achieved by using Method overloading, Method Overriding.
Method Overloading: A method can be same name with different parameter list (Achieved
while performing login functionality in the project)
Method Overriding: Duplicate method in child class will override the method in parent class.
INHERITANCE: Acquiring the properties from parent to child called inheritance by using extends
Keyword we can achieve inheritance.
Example: Generally, we extend the base class (Which has browser initialization and invoking to
URL codes) in real time scenarios when we work with framework
Usage: Code reusability.
Encapsulation also called data hiding it can be achieved by using private, getter and setter
methods (I have declared username, password elements as private in the @FindBy and
achieved these by using getter and setter methods)
13.Synchronization: It’s the concept where multiple threads trying to access same object at the
same time which leads to Data Inconsistency problem.
To solve this Data Inconsistency problem Synchronization will come into picture
If you want to synchronize only few lines of code then go for this synchronized block.
ii)using synchronized block:
synchronized(this)
{
body of the block
}
14. Thread: its light weight sub part of process (Thread is a smallest part of the program)
it will control by programmers.
Process: A program in execution is often referred to as process here threads are subpart of the
process.
it will control by operating system.
17. What is Abstract class and Abstract methods and how it is useful?
Abstract class: It is the class which has
Abstract methods (Just method declaration no definition) and
Non abstract methods (General methods)
2)STATIC keyword: Used for memory management and we save the memory because we no
need to create object to call either static method/variables. Static can be referred to the
common property
Static methods cannot be overridden.
3)THIS Keyword: Which is used to access current class objects this keyword will be used where
the scenario instance variables and local variables are same
4)FINAL Keyword: Once we declare variable or method or class with final keyword it is final, we
cannot change it.
finalize is a method is used to perform cleanup processing before object is garbage collected.
21.What is Exception and its hierarchy?
Exception class is a subclass of Throwable class and other than exception class there is another
subclass that is Error.
Error: An Error is abnormal condition that happens because of failures and it cannot be handled
by the Java programs.
E.g.: JVM is run out of memory normally programs cannot recover from it.
Examples:
FileNotFoundException
InterruptedException
IOException
Examples
SessionNotCreatedException
StaleElementReferenceException
NoElementFoundException
Throw Throws
Throw is a keyword used to throw an throws is a keyword used in the method
Exception explicitly within the function signature used to declare an exception
Which might get thrown by the function
while executing the program.
23. Why JAVA has both Compiler and Interpreter?
Because the source code is first compiled into binary byte code then this byte code runs on JVM
which usually software-based interpreter.
Collections
It’s a Framework where group of objects to be stored, manipulated and deleted
Collection--Interface whereas Collections--Class
Why collection came into picture means before collection we had concept of Arrays but with
arrays, we had some problems those are
1-Size problem
2-Type Problem
Collection Framework: -
----------------------
LIST(I)--which allows duplicates, Maintain Insertion order
SET(I)---Which does not allow duplicates, does not maintain insertion order
QUEUE(I)--FIFO
Comparable, Comparator--Both are used to Sort the elements in collection but the difference is
Comparable will do the default sorting whereas Comparator will do the customized sorting.
Array List: It’s a class and dynamic array in nature (resizable or growable array in size)
--It contains duplicate values
--It maintains insertion order
--NULL Insertion is possible
--Array List is better for Storing and Accessing data (because it implements RandomAccess
Interface)
--It creates an empty array list object with default initial capacity 10 once it reaches to full then
automatically it increases its size with 50% capacity
--How to make Array List Synchronized?
--Non-Synchronized:
ArrayList al = new ArrayList ();
--Synchronized
List I = Collections.synchronozedList(al);
void addFirst();
void addLast();
Object getFirst();
Object getLast();
Object removeFirst();
Object removeLast();
Vector: It’s a legacy class resizable or growable array its synchronized and thread safe compare
with Array List and uses some old methods
--It contains duplicate values
--It maintains insertion order
--NULL Insertion is possible
Methods uses in Vector class:
addElement ()
removeElementAt ()
removeAllElements ()
Stack: It’s a child class of vector and specially designed for Last In First Out order (LIFO)