Java for Testers – Java Tutorial Series for Selenium Testers
Welcome to our Core Java Tutorial series! This comprehensive Core Java for Testers Series is designed specifically for beginners who wants to learn Java and also for the Selenium testers who are learning Java. Understanding Java is essential, it empowers you to write automated test scripts, effectively interact with various testing tools, and significantly improve your testing efficiency.
Throughout this tutorial, we will cover fundamental concepts of core Java, tailored to the needs of testers. Whether you are a beginner or looking to refine your Java skills, this tutorial series will guide you through everything you need to know to become proficient in Java programming for testing purposes.
To start you automation testing journey, check out our Selenium WebDrive with Java Tutorial.
Java for Testers
Let’s see what we are going to learn in this Core Java Tutorial now
- 1. Java Introduction
- 2. JDK, JRE and JVM
- 3. Java Environment Setup
- 4. Java Syntax
- 5. Print in Java
- 6. User Input In Java
- 7. Comments in Java
- 8. Read From File
- 9. Write To File
- 10. Variables In Java
- 11. Variable Naming Convention in Java
- 12. Types of Variables in Java
- 13. Data Types in Java
- 14. Operators In Java
- 15. Control Flow Statements
- 16. Conditional Statements
- 17. if statement
- 18. Nested if statement
- 19. if else statement
- 20. Switch Case
- 21. For Loop
- 22. Enhanced For Loop
- 23. While Loop
- 24. Do While Loop
- 25. Continue Statement
- 26. Break Statement
- 27. OOPS Concept
- 28. Class
- 29. Object
- 30. Method
- 31. Modifiers
- 32. Access Modifiers
- 33. Constructor
- 34. Inheritance
- 35. Polymorphism
- 36. Method Overloading
- 37. Method Overriding
- 38. Abstraction
- 39. Abstract Class
- 40. Abstract Method
- 41. Interface in Java
- 42. Encapsulation
- 43. Arrays in Java
- 44. ArrayList in Java
- 45. How To Convert Array to ArrayList
- 46. Collections Framework in Java
- 47. Map in Java
- 48. LinkedList in Java
- 49. Exception Handling
- 50. Java Interview Questions
- 51. Java Quiz
Java Introduction:
Let’s see a brief introduction to Java. Java is a high-level programming language originally developed by Sun Microsystems in 1995. Java is a platform independent language. Yes, it runs on a multiple platforms such as UNIX, Windows, Mac OS. The Java language’s programming is based on the concept of OOP. We will see this in detail in later part of this Java Tutorial.
JDK, JRE and JVM:
We need to understand three terminologies for sure in Java such as JDK, JRE and JVM. Here I give basic idea about these terms in the next post we will see detailed explanation.
What is JDK?
JDK stands for Java Development Kit.
Using JDK, we can develop, compile and execute (run) new applications and also we can modify existing applications. We need to install JDK in developers machine where we want to develop new applications or modify existing applications.
JDK includes JRE and development tools (environment to develop, debug and monitor Java programs).
What is JRE?
JRE stands for Java Runtime Environment.
Using JRE, we can only execute already developed applications. We cannot develop new applications or modify existing applications.
As the name suggests, JRE only provides Runtime Environment.
What is JVM?
JVM stands for Java Virtual Machine. JVM drives the java code. Using JVM, we can run java byte code by converting them into current OS machine language.
Java Environment Setup:
Download and Install JAVA
Go to the below mentioned link and download the latest version of JAVA
http://www.oracle.com/technetwork/java/javase/downloads/index.html
Accept the license agreement and choose the right ‘JDK’ file to download based on your system requirement.
Once downloaded. Go ahead and verify the Java version. To do this, open command prompt and type “java -version” and hit enter
Java Syntax:
1. Java is a case sensitive language
Ex: NAME and name are not same as per Java Language
2. Java file name should be same as its Class name
3. Class name should start with upper case letter
4. Method name should start with lower case letter
5. Every statement should end with semi colon
6. Java program execution starts from main method which is mandatory in every program
public static void main(String [] args){
}
Print in Java:
In Java, we use print to output required text directly to the console of IDE
Syntax:
Simple print statement:
System.out.print(“Learning Java from SoftwareTestingMaterial”);
Simple print statement with new line:
System.out.println(“Learning Java from SoftwareTestingMaterial”);
Check this link to read more on Print in Java
User Input In Java:
Sometimes, we may face a situation where we need to get the input from the user in runtime. We use “Scanner” class to accept input from the user.
Syntax:
import java.util.Scanner; Scanner userInput = new Scanner(System.in); variable = userInput.next(); userInput.close();
User Input in Java with sample programs
Comments in Java:
In Java, we have two types of comments. We use comments to write some text within our code. Compiler will ignore these comments.
Syntax:
// Single line comment
/* Multi line comments – Line 1 Multi line comments – Line 2 */
Note: Comments in between the code gives more readability
Do you want to show auto generated code whenever you create a new class as shown below.
Follow the below steps:
I assume, you are using Eclipse IDE.
1. In eclipse, Go to Window – Preferences
2. From the left panel, Select Java – Code style – Code template
3. Under ‘Configure generated code and comments’, Expand Comments – Select Files and Click Edit and Enter your text and Click OK.
Whenever you create a new class, you can see comments.
Read From File:
To read a text file, we use FileReader and wrap it in a BufferedReader.
In the below example, we read a file named “FileToRead.txt” which is located in my local system and output the file line by line in my eclipse console.
Sample program on Read From File in Java
Write To File:
To create a new file and write text on it. We can write to a file using Java in different ways but I show you how to write text to a file using BufferedWriter.
Sample program on Write To File in Java
Variables In Java:
In Java, variable is a name given to a memory location and this variable is associated with a value.
int x = 99;
int – data type
x – variable
99 – value
variable x holds integer values and its current value is 99.
Let’s see how to declare variables in Java
Syntax to declare a variable in Java:
data_type variable = value;
Example:
int x = 99;
Variable Naming Convention in Java:
Earlier we have learnt that Java is a Case Sensitive Language. Even variables have their own naming convention to follow.
1. Variable name can starts with special characters such as _ or $
Example:
int $myAge;
2. Variable name should begin with lower case leter
Example:
Wrong way: int Age;
Correct way: int age;
3. If the variable name consists of more than one word, it’s a best practice to capitalize the first letter of each subsequent word.
Example:
Wrong way: int myage;
Correct way: int myAge;
4. Variable name should not contain white spaces
Example:
Wrong way: int my Age;
Correct way: int myAge;
Types of Variables in Java:
There are three types of variables in Java.
1. Local variable
2. Instance variable
3. Class/Static variable
Read more on Variables in Java with Sample programs
Data Types in Java:
Data types in java specify the size and type of values that can be stored in an identifier. There are two types of Data Types in Java.
1. Primitive Data Type
2. Non-primitive Data Type
Primitive Data Type:
There are 8 primitive data types such as byte, short, int, long, float, double, char, and boolean. Size of these 8 primitive data types wont change from one OS to other.
byte, short, int & long – stores whole numbers
float, double – stores fractional numbers
char – stores characters
boolean – stores true or false
Non-primitive Data Type:
Non-primitive data types include Classes, Interfaces and Arrays which we will learn in coming tutorials.
Sample program on Data Types in Java
Operators In Java:
Operators in Java are the special symbols that perform specific operations and then return a result.
Types of Operators in Java are
1. Arithmetic Operators (+, –, *, /, %)
2. Assignment Operators (=, +=, -=, *=, /=, %=)
3. Auto-increment Operator and Auto-decrement Operators (++, —)
4. Logical Operators (&&, ||, !)
5. Comparison (relational) Operators (==, !=, >, <, >=, <=)
6. Bitwise Operators (&, |, ^, ~, <<, >>)
7. Ternary Operator
Sample programs on Operators in Java
Control Flow Statements:
Following image shows you the subdivisions of Control Flow Statements in Java.
Conditional Statements:
Let’s see the following conditional statements
1. if statement
2. nested if statement
3. if-else statement
4. if-else-if statement
5. Switch Case Statement
Check this link to learn all the Conditional Statements with sample programs
if statement:
The if statement is the most basic of all the control flow statements. The if statement tells our program to execute a certain section of code only if a particular test evaluates to true.
Nested if statement:
An if statement inside another the statement. If the outer if condition is true then the section of code under outer if condition would execute and it goes to the inner if condition. If inner if condition is true then the section of code under inner if condition would execute.
if-else statement:
If a condition is true then the section of code under if would execute else the section of code under else would execute.
Switch Case:
The switch statement in Java is a multi branch statement. We use this in Java when we have multiple options to select. It executes particular option based on the value of an expression.
Switch works with the byte, short, char, and int primitive data types. It also works with enumerated types, the String class, and a few special classes that wrap certain primitive types such as Character, Byte, Short, and Integer.
For Loop:
The for statement in Java allows us to repeatedly loops until a particular condition is satisfied.
Syntax:
for (initialization; termination; increment){
//statement(s)
}
Detailed explanation on For Loop with sample program
Enhanced For Loop:
The Enhanced For Loop is designed for iteration through Collections and Arrays. This enhanced for loop makes our loops more compact and easy to read.
Syntax:
//temporary iterator variable is declared in the loop
for(dataType iteratorVariable : IterableObject){
//the individual element is held in the iterator variable
//to access the value, just use iteratorVariable
}
Enhanced For Loop with a sample program
While Loop:
The while statement continually executes a block of statements while a particular condition is true.
Syntax:
while (expression) {
// statement(s)
}
If the expression of while statement evaluates to true, then it executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false.
While Loop with Sample Program
Do While Loop:
The do-while is similar to the while loop. In do-while loop, the condition is evaluated after the execution of statements with in the do block at least once.
do
{
//statement(s);
} while(condition);
Do While Loop with Sample Program
Continue Statement:
The Continue Statement in Java is used to continue loop. It is widely used inside loops. Whenever the continue statement is encountered inside a loop, control immediately jumps to the beginning of the loop for next iteration by skipping the execution of statements inside the body of loop for the current iteration.
Syntax:
continue;
Continue Statement in Java with Sample Program
Break Statement:
The Break statement in Java is used to break a loop statement or switch statement. The Break statement breaks the current flow at a specified condition.
Note: In case of inner loop, it breaks just the inner loop.
Syntax:
break;
Break Statement with Sample Program
OOPS Concept:
OOPS Stands for Object Oriented Programming System. In this tutorial, I will introduce you to Class, Object, Constructor, Abstraction, Encapsulation, Inheritance, Polymorphism, Interface etc.,
Class:
A class is a blueprint or prototype from which objects are created. A class contains variables (data types) and methods (functions) to describe the behavior of an object.
class Class_Name{
member variables
methods
}
Object:
Object is a software bundle of related state and behavior. Objects have two characteristics namely state and behavior.
We can also say, Object is an entity that has state and behavior.
State: It represents value (data types/variables) of an object
Behavior: It represents the functionality (methods) of an object
Object is an instance of a class.
Sample:
class Computer{
String Maker;
int Model;
String Color;
void turnOn{
//statement(s)
}
void turnoff{
//statement(s)
}
}
Example:
State: Maker, Model, Color etc.,
Behavior: Turn on, Turn off etc.,
To understand what is a class and object in detail, let me give you a basic example related to a computer. Computer with Model and Price.
Assume, you have two computers of Apple and Lenovo. Now say the model of Apple is MacBook Pro and the model of Lenovo is Yoga. The price of Apple is $299 and the price of Lenovo is $99.
Computer is a class which has two attributes namely Model and Price. Apple and Lenovo are the objects of the class Computer.
Let’s see how to create an object:
Compter laptop = new Computer();
Class: Computer
Reference: laptop
Keyword: new
Constructor: Computer()
Object: new Computer()
Computer is a class name followed by the name of the reference laptop. Then there is a “new” keyword which is used to allocate memory. Finally, there is a call to constructor “Computer()”. This call initializes the new object “new Computer()”.
We create an Object by invoking the constructor of a class with the new keyword.
I hope, now you got to know how to create an object
Method:
Earlier we have seen Object is an entity which has both state and behavior. Here we are going to discuss about behavior of an Object. Method describes the behavior of an Object. A method consists of collection of statements which performs an action.
Methods are also known as procedures or functions
Let’s see an example of a method declaration.
public int sum(int a, int b, int c){
// method body
}
public void sum(int a, int b, int c){
// method body
}
Every method declaration must have return type of the method, a pair of parenthesis, and a body between braces
In general, method consists of 6 components.
Modifiers: private, public, and others
Return Type: The data type of the value returned by the method, or void if the method doesn’t return a value.
Method Name: Name of the method.
Built in methods are standard such as System.out.println();
User defined methods accept any names which a developer assigns.
Parameters inside parenthesis: list of parameters preceded by their data types and separated with a comma. If no parameters then you must specify an empty parenthesis.
Exception: Exceptions depends on the operation of the method
Method Body: Method body should be enclosed between braces
The signature of the above declared method is
Int sum(int a, int b, int c)
A method has a unique name within its class. However, a method might have the same name as other methods due to method overloading.
Let’s see how to call methods using an object.
class Computer{
// method
void turnOn{
//statement(s)
}
public static void main (String [] args){
// Created an object
Computer laptop = new Computer();
//Method called
laptop.turnOn();
}
}
Hope you have heard a phrase “Instantiating a class”. The phrase “Instantiating a class” means the same thing as “Creating an Object” which we did in the above program. Whenever you create an Object, it means you are creating an instance of a class, therefore “instantiating a class”.
Methods are of two types






