Selenium Online Training Video 4

Video 4 – Java Comments, Modifiers, Data Types, Variables and Operators

Java Programming for Selenium

1) Java Comments
2) Java Modifiers
3) Java Data Types
4) Java Variables
5) Java Operators

1) Java Comments

Comments are English words used for Code Documentation

Purpose of Comments:


To make the Code Readable
To make the code disable the code from Execution

Java Supports single line comment and multiple line comments.

Syntax:
a) Use // for Single line Syntax

b) Use /* ——————–

———————-

—————*/ for multiple line comments

Or
Select Statements / Steps
Source Menu (In Eclipse IDE) > Add Block Comment

Uncomment


Select Comment Block > Source Menu (In Eclipse IDE) > Remove Block Comment

Example:

public static void main(String[] args) {
// It is a Sample Program
int a, b; //Declaration of Variables
a=10; b=20; // Initialization
System.out.println(a+b);//30

/*if (a > b){

System.out.println(“A is a Big Number”);

}

else

{

System.out.println(“B is a Big Number”);

}*/

}
}

Usage of Comments in Test Automation

a) To write Test case Header
b) To write Method header
c) To explain Complex Logic etc…

2) Modifiers in Java

Two Categories of Modifiers in Java

a) Access Modifiers
b) Non -Access Modifiers

a) Access Modifiers

Access Modifiers can be used to define access control for classes, methods, and variables.

1) public

public access modifier is accessible every where

Ex:
public class Sample{
…..
}

public int a =10;

public int add (Parameters){
Method body…
}

2) private

The private access modifier is accessible only with in class

private int b=200;

3) default

if we don’t specify any modifier then It is treated as default,
this can be accessible only within package

Ex:
class Sample{
…..
}

4) protected

The protected modifier is accessible within package, outside of the package but through Inheritance.

protected class Sample{
….
}

b) Non -Access Modifiers

1) static

static modifier is used to create classes, methods, and variables.

static int a=10;

static int add (parameters){

————–

}

2) final

final modifier for finalizing classes, methods, and variables.

final int x =100;//Correct
……..
final int y;//Incorrect
y=200;

3) abstract

abstract modifier is used to create abstract classes and abstract methods.

abstract class Sample{
……..
}

abstract int add();
public int add(){
……..
Methods
………
}

Java Tutorial
Java Programming for Selenium

3) Java Data Types

What is Data Type?

Data Type is a classification of the type of data that a Variable or Constant or Method can hold in computer programming

Ex: Character, Number, Number with decimal values, Logical data, String, Date, Currency etc…

Note: Java supports explicit declaration of Data Types.
(We need to specify the Data Type before declaring Variables, Constants, and methods…)

Syntax:

a) Variables

dataType variableName;
Or
dataType variableName = value;
Or
dataType variable1Name, variable2Name, variable3Name;
or
dataType variable1Name=value, variable2Name= value, variable3Name=value;

Example:

int a;
a=10;

int b=100;

int c, d, e;
c=30; d=40; e=50;

int f=60, g=70, h=80;

b) Constants

Note: Variables and Constants are for holding the data, Variables may vary, but constants never change

int a;
a=100;//Correct for Variable
.
.
.
.
a=30; //Correct for Variable

final int b;
b=200; //Incorrect
……..
final int c =300;//Correct
.
.
.
c=400; //Incorrect

c) Method with Return Value

public int add (int a, int b, int c){
………………..
………………..
…………….
return…;
}

Two Categories of Data Types

1) Primitive Data Types
2) Non-primitive data Types

1) Primitive Data Types

(* Find the Number ranges for primitive data types)

a) Integer Data Types

i) byte (8 bits)
Minimum value is -128 (-2^7), Maximum value is 127 (inclusive)(2^7 -1)
Default value is 0

byte a=10;

ii) short (16 bits)
Minimum value is -32,768 (-2^15)
Maximum value is 32,767 (inclusive) (2^15 -1)
Default value is 0.

short b=1000;

iii) int (32 bits)
Minimum value is – 2,147,483,648 (-2^31)
Maximum value is 2,147,483,647(inclusive) (2^31 -1)
The default value is 0

int c =10000;
int d=12;
int e=1;

iv) long (64 bits)
Minimum value is -9,223,372,036,854,775,808(-2^63)
Maximum value is 9,223,372,036,854,775,807 (inclusive)(2^63 -1)
Default value is 0L

long f =1000000000000000;

b) Relational Data Types (Numbers with Decimal Places)

v) float (32 bits)

float x = 10.23f;

vi) double (64 bits)

double y = 1234.5678765;

c) Character Data Type

vii) character

char z = ‘A’;
char s = ‘1’;
char r = ‘*’;

d) Conditional Data Type

viii) boolean

boolean k = true/false;

2) Non Primitive data types / Reference data types

Non Primitive data types in Java are Objects (String, Array etc…)

Example:
Sample a = new sample();

123 – Integer Data Type
‘Y’ -Character
123.34 – float/double
“Selenium Testing” – String
“abc123*&^” – String
“123” – String
1 – Integer
‘1’ – Character

Converting Data / Data Conversion

Converting data from one type to another…

Assigning Values to Variables
1) Initialization – No Data Conversation
2) Reading –
Read Input (using Input Devices)
Read data from files
Read data from Application objects

When Data Conversion is required?

Whenever we read data then computer program considers that data as String type data, we need to convert the data in order to perform mathematical operations,
Note: Generally we convert String type to Integer Type/ Relational type,
We can’t convert Alpha bytes to numbers etc…

4) Java Variables

i) What is Variable?

A named memory location to store the temporary data within a program,

Two types of memory in Computer Environment
a) Primary Memory (RAM)
b) Secondary Memory (ROM- CD, DVD, HDD, USB etc…)

ii) Declaration of Variables

Java supports Explicit Declaration of Variables,

In Java;

dataType VariableName;
dataType Variable1Name, Variable2Name, Variable3Name;
dataType variableName= value;
dataType Variable1Name=value, Variable2Name=value, Variable3Name=value;

Example:
int a;
char b;
boolean c;
double d;
String e;
a=10;
b=’X’;
c= false;
d= 12.345;
e=”Selenium Testing”;

int f, g, h;
f=50; g=60; h=70;

int i=100;

int j=12, k=13, l=14;

iii) Assign values to Variables

Two types of Assign values to Variables
a) Initialization
int a =100;
or
int b;
b=200;
b) Reading
Read Input (Using Input Devices)
Read from Files (Text/Excel/Database file…)
*Read from Application Objects

iv) Variable naming Restrictions

a) Variables are case sensitive

in Java
int a;
int B;
a=100;
B=200;
System.out.println(A); //Incorrect
System.out.println(a); //Correct
System.out.println(B); //Correct
System.out.println(b); //Incorrect

Dim a
.
.
A=100
.
.
Msgbox a’100 (Correct Syntax)

b) Java Variable names should start with a letter or $ or _

Ex:
myvar
MYVAR
$myvar
_myvar
7myvar //Incorrect
*myvar //Incorrect
myvar7

c) Variable names should not match with Java Keywords and Reserved words

if, for, import, while, new…..(Keywords)
true, false…..(Reserved Words)

int a;//Correct
int for; //Incorrect

d) Must be unique in the scope of declaration (No duplicate variables in a program)

e) Must not exceed 255 characters

Three types of variables in Java,

a) Local Variables
Local Variables are declared in methods, or blocks.

b) Instance Variables
Instance Variables are declared in a class but outside of a method or any block.
Instance variables are used by Objects to store their states.

c) Class / Static Variables
Static Variables are declared as static, these can’t be local

If you declare Instance variable before main method then it can be used before
main method only.

If you declare Instance variable after main method then it can be used after main method only.

Example:
public class VariablesOperators {
static int a =10;

public int salary(){
int mySalary = 10000 +2000+1500;
mySalary = mySalary +a;
return mySalary;
}
public static void main(String[] args) {
//Static / Class Variable
int b=20; //Instance variable
System.out.println(b);//20
VariablesOperators obj= new VariablesOperators();
int e = obj.salary();
System.out.println(e);//13510

if (b>a){
int x=123;
System.out.println(a);//10
System.out.println(b);//20
System.out.println(x);//123
}
System.out.println(a);//10
System.out.println(b);//20
//System.out.println(x);//123//Error
}
}

//Calling methods
obj.salary();
obj – Object
salary – Method

5) Java Operators

Operators are used to perform Arithmetic, Comparison, and Logical Operations,

Categories of Java Operators,

a) Arithmetic Operators

b) Assignment Operators

c) Comparison Operators

d) Logical Operators

a) Arithmetic Operators

Arithmetic Operations,

Addition, Subtraction, Multiplication, Division, Modules, Exponentiation etc…

1) Addition + (Addition, String Concatenation)
int a=10, b=20;
int c = a+b; //Addition

String d = “Selenium”;
String e = ” Testing”;
String f = d+e; //”selenium Testing”

2) Subtraction – (Subtraction, Negation)

int a=20, b=10;
int c = a-b;//10 (Subtraction)

int d = -100;//negation

3) Multiplication *

4) Division /

5) Modules %

6) Increment ++

7) Decrement —

Example:

public class VariablesOperators {
public static void main(String[] args) {
int a=10, b=5;
String c= “Selenium”, d= “Testing”;

System.out.println(“Addion of a, b is: ” + (a+b));//Addition of a, b is: 15
System.out.println(a+b);//15
System.out.println(c+d);//SeleniumTesting
System.out.println(a-b);//5
System.out.println(a*b);//50
System.out.println(a/b);//2
System.out.println(a%b);//0

b=10;
a = ++b;
System.out.println(a);//11

b=10;
a = –b;
System.out.println(a);//9

}
}

b) Assignment Operators

1) Assignment =

2) Add and Assign +=

3) Subtract and Assign -=

4) Multiply and Assign *=

Example:

public class VariablesOperators {
public static void main(String[] args) {
int a=10;

System.out.println(a);//10
a= a+10;
System.out.println(a);//20

a=10;
a += 10;
System.out.println(a);//20

a -=10;
System.out.println(a);//10

a *= 10;
System.out.println(a);//100
}
}

c) Relational / Comparison Operators

1) ==

2) !=

3) >

4) >=

5) <

6) <=

Note: Relational Operators return Boolean / Logical (true/false) Result

Example:
public class VariablesOperators {
public static void main(String[] args) {
int a=10, b=20;

System.out.println(a>b);//false
System.out.println(a>=b);//false

System.out.println(a<b);//true
System.out.println(a<=b);//true

System.out.println(a==b);//false
System.out.println(a!=b);//true
}
}

d) Logical Operators

1) Logical Not Operator !
2) Logical And operator &&
3) Logical Or Operator ||

Result Criteria for Not Operator:

Operand 1 Operand 2 Result
……………………………………………………..
true true false
true false true
false true true
false false true
……………………………………………………..
public class VariablesOperators {
public static void main(String[] args) {
boolean a =true, b=false;

System.out.println(! (a && b));//true
System.out.println(a && b);//false
System.out.println(a || b);//true
}
}

Result Criteria for And Operator:

Operand 1 Operand 2 Result
……………………………………………………..
true true true
true false false
false true false
false false false
……………………………………………………..
Result Criteria for Or Operator:

Operand 1 Operand 2 Result
……………………………………………………..
true true true
true false true
false true true
false false false
……………………………………………………..
Example:

int a =1000, b=500, c=900;

if ((a>b) && (a>c)){
System.out.println(“A is a Big Number”);
}
else
{
System.out.println(“A is Not a Big Number”);
}
}
}
……………………………………………………..
Example 2:
int a =100, b=500, c=700;

if ((a>b) && (a>c)) {
System.out.println(“A is a Big Number”);
}
else
{
System.out.println(“A is not a Big Number”);
}
……………………………………………………..
if ((a>b) || (a>c)) {
System.out.println(“A is a Big Number”);
}
else
{
System.out.println(“A is not a Big Number”);
}
}
}
……………………………………………………..
int a =10, b=50, c=70;

if (!((a>b) && (a>c))) {
System.out.println(“A is a Big Number”);
}
else
{
System.out.println(“A is not a Big Number”);
}
……………………………………………………..

Follow me on social media: