Java Basics for Testers

Java is an essential language for Software testers as Selenium using Java is one of the best pair used in Test Automation. Here are the basics that every tester should be aware of: 
Java Basics for Testers

01 Datatypes & Variables in Java

In simple words, A variable is a key that holds any value till the program lifecycle. A variable value can change as per manipulations during program execution.
A datatype defines the characteristics of a variable i.e what type of value can be stored in it.

A datatype can be Integer, String, character, float, double, boolean etc.

How to name a variable? 
  1. A variable name can be a single alphabet (A-Z).
  2. Variable should start with an alphabet, ‘_’ or ‘$’ only.
  3. Variable name can include digits but cannot start with a digit
  4. The name of a variable cannot include Java keywords.

For example:

String sName = “Ishan”; 
// A String needs to be represented within Double quotes
int iNum = 10; 
// An integer can store 4 bytes. Range: -2,147,483,648 to 2,147,483, 647
boolean bResult = true; 
// Boolean datatype can store either true/false

Datatypes: String, iNum, bResult
Variables: sName , iNum, bResult 
Values: “Ishan”, 10, true

02 Operators in Java
Operators are user to perform specific operations / calculations among one or more operands / variables.

Classification of Java Operators:

Unary operators: Needs one Operand. ex: int next = i++;
Binary operators: Needs two Operands. ex: int sum = a + b;
Ternary operators: Needs three Operands. ex: if(next == sum) ? true : false

Commonly used Operators in Java:

  • Assignment Operators : '='
  • Conditional Operator : '?'
  • Relational Operators : '>', '<', '>=', '<=', '==', '!='
  • Arithmetic Operators : '-', '+', '*', '/', '%', '++', '--'
  • Logical Operators : '&&', '||', '&', '|', '!', '^'
Sample program illustrating Operators in Java: 
03 Decision Making in Java
Decision making is a very commonly used concept in any programming language. It directs the compiler to choose either of the paths based on a condition for program execution.

We can make decisions in Java using either of two statements:

1. The If statement

If statement can be further used as:

(a) if-then statement

int iNumber1=10;
int iNumber2 = 20;
if (iNumber1+iNumber2<40)
System.out.println("Sum is less than forty");

(b) if-then-else & if-else_if-else
int iNumber1=10;
int iNumber2 = 20;
if (iNumber1+iNumber2<40)
System.out.println("Sum is less than forty");
System.out.println("Sum is greater than forty");

(c) if-else_if-else
int iNumber1=10;
int iNumber2 = 20;
if (iNumber1+iNumber2<40)
System.out.println("Sum is less than forty");
else if ((iNumber1+iNumber2>40))
System.out.println("Sum is greater than forty");

2. The Switch statement

The switch case comes handy In case you have multiple cases. The switch statement is multi-directional . It easily routes program execution to different parts of code based on expression's value.

public class SwitchExample {
    public static void main(String[] args) {
        int vowel = 4;
        String vowelString;
        switch (vowel) {
            case 1:  vowelString = "a";
            case 2:  vowelString = "e";
            case 3:  vowelString = "i";
            case 4:  vowelString = "o";
            case 5:  vowelString = "u";
            default: vowelString = "Invalid vowel";
       //This will print vowel "o" as standard output!

04 Arrays in Java
A variable that can store multiple values of same datatype is called an Array in Java. An Array index starts at 0.
Syntax: DataType ArrayName []= New DataType [Size];

For example:
int iArray [] = new int [5];
int iArray [0] = 1;
int iArray [1] = 2;
int iArray [2] = 3;
int iArray [3] = 4;
int iArray [4] = 5;
Same Array can also be declared as: int iArray[] = ['1','2','3','4','5'] 
Sample program illustrating  Arrays in Java:
05 Loops in Java
Loops are used to customize and execute repetitive piece of code using single instruction set. This helps in optimization and reducing redundancy of code. Loops are used in order to to execute same block of code again and again as per required conditions. Loops same a lot of Coding and execution time.

Types of loops and keywords in Java
  • The for loop : This executes the statement based on no. of iterations.
  • Break keyword : Helps in exiting the loop.
  • Continue keyword  : Used to skip a condition in Loop.
  • The while loop : This executes the statement based on unknown no. of iterations.
  • The do-while loop : This executes the statement at least once.
  • Enhanced for loop : Modified for loop. This loop works with arrays, Hash-maps of any Datatype.
A loop within a loop is called a nested loop.

06 Classes & Objects in Java
Classes and Objects play a key role in every Object Oriented programming language. Classes lets us group simple data structures and create complex data structures.

Objects in Java?
Object Oriented Programming defines an object as an instance of a class. An object can be anything like a car, dog, door window etc. which performs certain activities. 
Objects have two mandatory characteristics: state and behavior.
For example: A motorbike has state (brand, color, model etc.) and behavior (braking, accelerating etc).

In programming languages objects maintain state in variables and executes its behavior using methods. So from  above example can say that an object Motorbike is a collection of methods ( behavior – braking, accelerating)   and variables (state – make, brand, color etc.).

Classes in Java?
Classes are the blueprints for creating objects. By blueprints, it means classes always have same structure and implementation.
Class is created while creating program whereas, objects are created at run-time.
Sample program illustrating Classes & Objects in Java:
07 Class Constructors in Java
In Java Constructors are used to set initial values for variables. The compiler calls the constructor first when an object is created. A Constructor is called automatically at run-time when the compiler creates a new object.
Constructor methods are named exactly as the class name.
Unlike normal methods, class constructors don’t need to a return a specific datatype or a value.
The Compiler considers a default constructor for a class having no constructor defined explicitly.

Why are Constructors required?

In case exposing class variables to the main program is not secure, class variables can be declared as private i.e. inaccessible from other classes. By defining Constructors main function would access the constructor instead of accessing class variables. 
Sample program illustrating Constructors in Java: 

08 String Class in Java

What is String?

A String is a group of alphabets. It's a Class unlike datatypes  ‘int & char‘. String is one of the most important and commonly used class in Java and Selenium. 
A String object can not be changed, once created i.e. it's an immutable class.

String Manipulations:

String class has got many methods used very frequently in Selenium Automation using Java.

Sample program illustrating String manipulations in Java: 

Post a Comment