Java Notes For Selenium
Java Notes For Selenium
WWW.PAVANONLINETRAININGS.COM
Agenda
▪ Java Introduction
▪ Java Variables
▪ Java Operators
WWW.PAVANONLINETRAININGS.COM
What is Java?
▪ It is used for:
– Mobile applications (specially Android apps)
– Desktop applications
– Web applications
– Web servers and application servers
– Games
– Database connection
– And much, much more!
WWW.PAVANONLINETRAININGS.COM
Why Use Java?
WWW.PAVANONLINETRAININGS.COM
JDK, JRE and JVM
▪ JDK contains everything that will be required to develop and run Java application.
▪ JRE contains everything required to run Java application which has already been compiled. It
doesn’t contain the code library required to develop Java application.
▪ JVM is a virtual machine which works on top of your operating system to provide a recommended
environment for your compiled Java code. JVM only works with bytecode. Hence you need to
compile your Java application(.java) so that it can be converted to bytecode format (also known as
the .class file).
▪ Which then will be used by JVM to run an application. JVM only provide the environment needed
to executed Java Bytecode.
WWW.PAVANONLINETRAININGS.COM
JDK, JRE and JVM
WWW.PAVANONLINETRAININGS.COM
Download & Install Java
1) Download Link
▪ http://download.oracle.com/otn-
pub/java/jdk/9.0.4+11/c2514751926b4512b076cc82f959763f/jdk-9.0.4_windows-x64_bin.exe
WWW.PAVANONLINETRAININGS.COM
2) Once the download is complete, run the exe for install JDK. Click Next
WWW.PAVANONLINETRAININGS.COM
Java Environment Setup
WWW.PAVANONLINETRAININGS.COM
Eclipse IDE
▪ https://www.eclipse.org/downloads/download.php?file=/oomph/epp/photon/R/eclipse-inst-
win64.exe
WWW.PAVANONLINETRAININGS.COM
Download and Start Eclipse IDE
1
2
WWW.PAVANONLINETRAININGS.COM
5
WWW.PAVANONLINETRAININGS.COM
WWW.PAVANONLINETRAININGS.COM
Create a Java Project
WWW.PAVANONLINETRAININGS.COM
IntelliJ IDE
WWW.PAVANONLINETRAININGS.COM
Java Variables
▪ What is a Variable?
▪ Every variable is assigned a data type which designates the type and quantity of value it can hold.
WWW.PAVANONLINETRAININGS.COM
Java Variables(Cont..)
WWW.PAVANONLINETRAININGS.COM
Java Data Types
WWW.PAVANONLINETRAININGS.COM
Java Operators
WWW.PAVANONLINETRAININGS.COM
Assignment
1. Write a Java program to print 'Hello' on screen and then print your name on a separate line.
▪ Expected Output :
▪ Hello
▪ Pavan
2. Write a Java program to print the sum of two numbers.
▪ Test Data: 4 + 36
▪ Expected Output : 50
4. Write a Java Program to swap two numbers without using third variable.
WWW.PAVANONLINETRAININGS.COM
Agenda
▪ Control Statements
▪ Conditional/Statements(Selection Statements)
▪ Jump Statements
WWW.PAVANONLINETRAININGS.COM
Control Statements
WWW.PAVANONLINETRAININGS.COM
If..else
WWW.PAVANONLINETRAININGS.COM
Switch..case
▪ The switch statement defines multiple paths for execution of a set of statements. It is a better
alternative than using a large set of if-else statements as it is a multi-way branch statement.
▪ In this Switch case flowchart, the code will respond in the following steps:
1. First of all it will enter the switch case which has an expression.
2. Next it will go to Case 1 condition, checks the value passed to the condition. If it is true, Statement block
will execute. After that, it will break from that switch case.
3. In case it is false, then it will switch to the next case. If Case 2 condition is true, it will execute the
statement and break from that case, else it will again jump to the next case.
4. Now let’s say you have not specified any case or there is some wrong input from the user, then it will go
to the default case where it will print your default statement.
WWW.PAVANONLINETRAININGS.COM
Switch..case (Cont..)
WWW.PAVANONLINETRAININGS.COM
While loop
WWW.PAVANONLINETRAININGS.COM
Do..while loop
WWW.PAVANONLINETRAININGS.COM
For loop
WWW.PAVANONLINETRAININGS.COM
When we use while, do..while & for loops
▪ Scenario 1: If u want to travel by your own vehicle(two wheeler or four wheeler),You should know
how much petrol available in your vehicle and you know how much distance to travel.
▪ Scenario 2: If u want to travel in a FLIGHT, You should buy a ticket then only you are eligible to
enter into the Flight.
▪ Scenario 3: If u want to travel in BUS, You can board the bus then buy the ticket.
WWW.PAVANONLINETRAININGS.COM
Jump Statements
▪ Jump statement: Jump statement are used to transfer the control to another part of your
program. These are further classified into – break and continue.
WWW.PAVANONLINETRAININGS.COM
Break statement
WWW.PAVANONLINETRAININGS.COM
Continue statement
WWW.PAVANONLINETRAININGS.COM
Input From the User
▪ Scanner class is to take the input from user which is imported from java.util.Scanner package.
Scanner input = new Scanner(System.in);
WWW.PAVANONLINETRAININGS.COM
Assignment
1. Write a Java program to get a number from the user and print whether it is positive or negative
3. Write a Java program that takes a year from user and print whether that year is a leap year or
not.
4. Write a Java to display the multiplication table of a given integer using for loop.
5. Write a Java program count the number of digits of the number using while loop.
7. Write a Java program to check Number is Palindrome or not using while loop.
8. Write a Java Program to print factorial of a given number using while loop, for loop.
WWW.PAVANONLINETRAININGS.COM
Agenda
▪ Arrays
▪ Strings
WWW.PAVANONLINETRAININGS.COM
What are Java Arrays?
▪ An array is a container object that holds a fixed number of values of a single type.
▪ The length of an array is established when the array is created. After creation, its
length is fixed.
▪ There are 2 types of arrays
1. Single Dimensional
2. Two Dimensional(Double Dimensional)
WWW.PAVANONLINETRAININGS.COM
Single Dimensional Array
▪ Declare array
WWW.PAVANONLINETRAININGS.COM
Multi Dimensional Array
• Declare array
WWW.PAVANONLINETRAININGS.COM
Assignment (Arrays)
WWW.PAVANONLINETRAININGS.COM
Assignment (Arrays) – Searching & Sorting
WWW.PAVANONLINETRAININGS.COM
Strings
▪ length(): It returns count of total number of characters present in the String.
▪ concat() : Combines a specific string at the end of another string and ultimately returns a
combined string. It is like appending another string.
String s=“Welcome”
s.concat(s1) Welcome To Java
String s1=“ To Java”
▪ trim() : The java string trim() method removes the leading and trailing spaces.
WWW.PAVANONLINETRAININGS.COM
Strings
▪ charAt(): Returns a char value at the given index number. The index number starts from 0.
▪ contains() : Searches the sequence of characters in this string. It returns true if sequence of char values are
found in this string otherwise returns false.
▪ equals() : Compares the two given strings based on the content of the string. If any character is not matched, it
returns false. If all characters are matched, it returns true.
WWW.PAVANONLINETRAININGS.COM
Strings
▪ equalsIgnoreCase() : Compares two string on the basis of content but it does not check the case
like equals() method. In this method, if the characters match, it returns true else false.
▪ replace(): Returns a string, replacing all the old characters or CharSequence to new characters.
There are 2 ways to replace methods.
WWW.PAVANONLINETRAININGS.COM
Strings
Strings
▪ Substring() : Returns substring of a string based on starting index and ending index.
Starting Index 0 1 2 3 4 5 6
s W E L C O M E
Ending Index
1 2 3 4 5 6 7
WWW.PAVANONLINETRAININGS.COM
Strings
Strings
WWW.PAVANONLINETRAININGS.COM
Assignment (Strings)
WWW.PAVANONLINETRAININGS.COM
Agenda
▪ Java Methods
▪ Java Constructor
WWW.PAVANONLINETRAININGS.COM
Java Class & Object
Object1
class Employee
{ Employee emp1=new Employee();
int eid; emp1.eid=1020;
String ename; emp1.ename="John";
double sal; Variables emp1.sal=80000;
String job; emp1.job="Manager";
void display()
emp1.display();
{ Object2
System.out.println(eid);
Class
System.out.println(ename);
Employee emp2=new Employee();
System.out.println(sal);
System.out.println(job); emp2.eid=1021;
emp2.ename="David";
} Methods emp2.sal=50000;
void bonus()
{ emp2.job="Tech Assistant";
System.out.println((sal *10) /100);
} emp2.display();
}
WWW.PAVANONLINETRAININGS.COM
Class & Object
WWW.PAVANONLINETRAININGS.COM
Class & Object
▪ We can have multiple classes in different java files or single java file.
Student.java Student1.java
WWW.PAVANONLINETRAININGS.COM
Class & Object
WWW.PAVANONLINETRAININGS.COM
Class & Object
Student.java Student2.java
WWW.PAVANONLINETRAININGS.COM
Class & Object
Student.java Student3.java
WWW.PAVANONLINETRAININGS.COM
Class & Object
Student.java Student4.java
WWW.PAVANONLINETRAININGS.COM
Java Methods
▪ A method is a set of code which is referred to by name and can be called (invoked) at any point in
a program simply by utilizing the method's name.
Case2
Case3
Case4
WWW.PAVANONLINETRAININGS.COM
Java Constructor
▪ Constructor in java is a special type of method that is used to initialize the object.
Constructor
Default Parameterized
WWW.PAVANONLINETRAININGS.COM
Method V/s Constructor
Method
• Need to call method explicitly.
WWW.PAVANONLINETRAININGS.COM
Assignment
WWW.PAVANONLINETRAININGS.COM
Agenda
▪ Overloading
▪ this keyword
WWW.PAVANONLINETRAININGS.COM
Method Overloading
▪ Method Overloading in Java is a concept related to Object Oriented Programming (OOP). Java
supports overloading of methods and can distinguish between different methods with method
signatures. A situation, wherein, in the same class there are two or more methods with same
name, having different functions or different parameters, it is called Method Overloading.
WWW.PAVANONLINETRAININGS.COM
Overloading
WWW.PAVANONLINETRAININGS.COM
62
▪ Yes, by method overloading. You can have any number of main methods in a class by
method overloading. But JVM calls main() method which receives string array as
arguments only.
WWW.PAVANONLINETRAININGS.COM
this keyword
▪ The this keyword can be used to refer current class instance variable. If there is ambiguity between the instance variables and
parameters, this keyword resolves the problem of ambiguity.
Output: Output:
0 10
0 20
WWW.PAVANONLINETRAININGS.COM
static
static
variable method
WWW.PAVANONLINETRAININGS.COM
static
Employee
int empno;
String ename;
int deptno;
void bonus()
WWW.PAVANONLINETRAININGS.COM
static
Employee
int empno;
String ename;
static int deptno=10; deptno=10
void bonus()
WWW.PAVANONLINETRAININGS.COM
static variables and methods
static Non-static
static methods
Non-static methods
Direct Access
Through Object
WWW.PAVANONLINETRAININGS.COM
System.out.println()
Test.s.length(); System.out.println();
WWW.PAVANONLINETRAININGS.COM
Assignment
➢ int sum(int x, int y) : Should accept two integer parameters and returns sum of two numbers.
➢ int sum(int x, int y, int z) : Should accept three integer parameters and returns sum of three
numbers.
➢ double sum(double x, double y) : Should accept two double type parameters and returns sum
of two numbers.
➢ double sum(double x, double y, double z) : Should accept three double type parameters and
returns sum of three numbers.
▪ Now, create object for Calculations class ‘cal’ then call different methods by passing different
inputs.
WWW.PAVANONLINETRAININGS.COM
Agenda
▪ Java Inheritance
▪ Method Overriding
▪ super Keyword
▪ final Keyword
WWW.PAVANONLINETRAININGS.COM
Inheritance
▪ In OOP, computer programs are designed in such a way where everything is an object that interact with one
another. Inheritance is one such concept where the properties of one class can be inherited by the other.
▪ It helps to reuse the code and establish a relationship between different classes.
▪ A class which inherits the properties is known as Child Class whereas a class whose properties are inherited is
known as Parent class.
WWW.PAVANONLINETRAININGS.COM
Types of Inheritance
WWW.PAVANONLINETRAININGS.COM
Single Inheritance
▪ In single inheritance, one class inherits the properties of another. It enables a derived class to
inherit the properties and behavior from a single parent class.
▪ This will in turn enable code reusability as well as add new features to the existing code.
▪ Here, Class A is your parent class and Class B is your child class which inherits the properties and
behavior of the parent class.
WWW.PAVANONLINETRAININGS.COM
Multilevel Inheritance
▪ When a class is derived from a class which is also derived from another class, i.e. a class having
more than one parent class but at different levels, such type of inheritance is called Multilevel
Inheritance.
▪ If we talk about the flowchart, class B inherits the properties and behavior of class A and class C
inherits the properties of class B. Here A is the parent class for B and class B is the parent class for
C. So in this case class C implicitly inherits the properties and methods of class A along with Class
B. That’s what is multilevel inheritance.
WWW.PAVANONLINETRAININGS.COM
Hierarchical Inheritance
▪ When a class has more than one child classes (sub classes) or in other words, more than one child
classes have the same parent class, then such kind of inheritance is known as hierarchical.
▪ If we talk about the flowchart, Class B and C are the child classes which are inheriting from the
parent class i.e Class A.
WWW.PAVANONLINETRAININGS.COM
Hybrid Inheritance
WWW.PAVANONLINETRAININGS.COM
Method Overriding
▪ If subclass (child class) has the same method as declared in the parent class, it is known
as method overriding in java.
WWW.PAVANONLINETRAININGS.COM
super keyword
WWW.PAVANONLINETRAININGS.COM
Super Keyword
WWW.PAVANONLINETRAININGS.COM
Super Keyword
WWW.PAVANONLINETRAININGS.COM
Super Keyword
WWW.PAVANONLINETRAININGS.COM
final Keyword
▪ The final keyword in java is used to restrict the user. The java final keyword can be
used for variables, methods and classes.
– variable
– method
– class
WWW.PAVANONLINETRAININGS.COM
Java final variable
▪ If you make any variable as final, you cannot change the value of final variable(It
will be constant).
WWW.PAVANONLINETRAININGS.COM
Java final method
WWW.PAVANONLINETRAININGS.COM
Java final class
WWW.PAVANONLINETRAININGS.COM
Assignment
▪ Assignment-1
▪ Create a class ‘Teacher’ which contains following variables and methods
– designation = "Teacher";
– collegeName = "BusyQA";
– does() → Teaching
▪ Create another class ‘ComputerTeacher’ which extends ‘Teacher’ class then create
objects then call methods.
WWW.PAVANONLINETRAININGS.COM
Agenda
▪ Java Interfaces
▪ Java Packages
▪ Access Modifier’s
WWW.PAVANONLINETRAININGS.COM
Java Interface
WWW.PAVANONLINETRAININGS.COM
Java Interface
WWW.PAVANONLINETRAININGS.COM
Multiple Inheritance in Java by Interface
interface interface interface interface
implements extends
interface
class implements
class
WWW.PAVANONLINETRAININGS.COM
Hybrid inheritance in java by interface
Class A
Interface B1 Interface B2
Class C
WWW.PAVANONLINETRAININGS.COM
Selenium WebDriver is an interface
WebDriver
WWW.PAVANONLINETRAININGS.COM
Java Packages
WWW.PAVANONLINETRAININGS.COM
Access package from another package
▪ There are two ways to access the package from outside the package.
• import package.*;
• import package.classname;
WWW.PAVANONLINETRAININGS.COM
Access Modifiers in java
▪ The access modifiers in java specifies accessibility (scope) of a data member, method,
constructor or class.
▪ There are 4 types of java access modifiers:
• private
• default
• protected
• public
WWW.PAVANONLINETRAININGS.COM
private access modifier
WWW.PAVANONLINETRAININGS.COM
default access modifier
▪ If you don't use any modifier, it is treated as default by default. The default modifier is
accessible only within package.
▪ * In the above example, the scope of class A and its method msg() is default so it cannot be
accessed from outside the package.
WWW.PAVANONLINETRAININGS.COM
protected access modifier
▪ The protected access modifier is accessible within package and outside the package but through
inheritance only.
▪ The protected access modifier can be applied on the data member, method and constructor. It
can't be applied on the class.
WWW.PAVANONLINETRAININGS.COM
public access modifier
▪ The public access modifier is accessible everywhere. It has the widest scope among all
other modifiers.
WWW.PAVANONLINETRAININGS.COM
Access modifiers
Access Modifier within class within package outside package by outside package
subclass only
Private Y N N N
Default Y Y N N
Protected Y Y Y N
Public Y Y Y Y
WWW.PAVANONLINETRAININGS.COM
Assignment
WWW.PAVANONLINETRAININGS.COM
Agenda
▪ Exception Handling
WWW.PAVANONLINETRAININGS.COM
Java Exceptions
▪ In java, exception is an event that disrupts the normal flow of the program.
Exception
Checked Un-Checked
WWW.PAVANONLINETRAININGS.COM
Un Checked Exceptions
▪ Exceptions that are NOT checked by compiler are called Un-Checked Exceptions.
▪ Un checked Exceptions successfully compiled by Java compiler.
▪ At run time it throws exception.
▪ Examples:
– ArithmeticException
– NullPointerException
– NumberFormatException
– ArrayIndexOutOfBoundsException
WWW.PAVANONLINETRAININGS.COM
Common Un-Checked exceptions
String s="abc";
int i=Integer.parseInt(s); NumberFormatException
WWW.PAVANONLINETRAININGS.COM
Checked Exceptions
▪ Examples:
– InterruptedException
– IOException
– FileNotFoundException etc.
WWW.PAVANONLINETRAININGS.COM
Common Checked exceptions
Thread.sleep(3000); InterruptedException
WWW.PAVANONLINETRAININGS.COM
Java Exception Handling Keywords
▪ try
▪ catch
▪ finally
▪ throws
WWW.PAVANONLINETRAININGS.COM
Java try..catch block
▪ Java try block is used to enclose the code that might throw an exception.
▪ It must be applied at statement level within the method.
▪ Java try block must be followed by either catch or finally block.
▪ Used for both Un-checked and Checked Exceptions.
▪ Java catch block is used to handle the Exception. It must be used after the try block only.
▪ You can use multiple catch block with a single try.
Syntax of java try-catch
WWW.PAVANONLINETRAININGS.COM
Problem without exception handling
WWW.PAVANONLINETRAININGS.COM
Solution by exception handling
WWW.PAVANONLINETRAININGS.COM
Java Multi catch block
▪ If you have to perform different tasks at the occurrence of different Exceptions, use java multi
catch block.
Output:task1 completed
rest of the code...
WWW.PAVANONLINETRAININGS.COM
Java finally block
▪ Java finally block is a block that is used to execute important code such as closing connection,
stream etc.
▪ Java finally block is always executed whether exception is handled or not.
▪ Java finally block follows try or catch block.
WWW.PAVANONLINETRAININGS.COM
Usage of Java finally
▪ Cases
1. Exception doesn't occur.
2. Exception occurs and not handled.
3. Exception occurs and handled.
WWW.PAVANONLINETRAININGS.COM
Case 1: Java finally example where exception doesn't occur
Output:5
finally block is always executed
rest of the code...
WWW.PAVANONLINETRAININGS.COM
Case 2: Java finally example where exception occurs and not handled.
WWW.PAVANONLINETRAININGS.COM
Case 3: Java finally example where exception occurs and
handled.
WWW.PAVANONLINETRAININGS.COM
throws
WWW.PAVANONLINETRAININGS.COM
throws – Example1
WWW.PAVANONLINETRAININGS.COM
throws – Example2
WWW.PAVANONLINETRAININGS.COM
Un-Checked Checked Method Level Within the method
Try..Catch Y Y N Y
throws N Y Y N
WWW.PAVANONLINETRAININGS.COM
Assingment
1. Write a java program for the following and handle exceptions by using try..catch and
finally blocks.
• Any number divide by zero.
• int a[]=null;
• a.length
• String s="abc";
• int i=Integer.parseInt(s);
WWW.PAVANONLINETRAININGS.COM
Agenda
▪ ArrayList
▪ HashMap
▪ JDBC
WWW.PAVANONLINETRAININGS.COM
ArrayList
▪ ArrayList is pre defined class in Java used for dynamic array for storing elements.
▪ ArrayList can contains duplicate elements.
▪ We can add, insert and remove elements from ArrayList.
Syntax:
WWW.PAVANONLINETRAININGS.COM
Java ArrayList Example1
WWW.PAVANONLINETRAININGS.COM
Java ArrayList Example2
WWW.PAVANONLINETRAININGS.COM
HashMap
WWW.PAVANONLINETRAININGS.COM
Java HashMap Example
WWW.PAVANONLINETRAININGS.COM
JDBC – Java Database Connectivity
▪ Java JDBC is a java API to connect and execute query with the database.
WWW.PAVANONLINETRAININGS.COM
Database and SQL
WWW.PAVANONLINETRAININGS.COM
Database Components
▪ Database Client
– CLI
– GUI
▪ Database Server
WWW.PAVANONLINETRAININGS.COM
4 Steps to connect to the database in java
▪ Creating connection
▪ Creating statement
▪ Executing queries
▪ Closing connection
WWW.PAVANONLINETRAININGS.COM
JDBC Example1
WWW.PAVANONLINETRAININGS.COM
JDBC Example2
WWW.PAVANONLINETRAININGS.COM