Java is most commonly used language used with Selenium.
I will explain the basics of Java to get you started with Selenium.
What is Java?
Java is a general-purpose programming language, which is concurrent; class based and object oriented language. Java follows the concept of “write once and run anywhere (WORA) which means that compiled java code can be run on all different platforms that support java without the need of recompilation.
The basic step towards understanding any programming language is to know the data type.
What is a Data Type?
A Data Type means what type of data the variable can represent. Data types are divided into two groups:-
- Primitive data types – includes byte, short, int, long, float, double, boolean and char
- Non-primitive data types – such as String, Arrays and Classes
Data Type |
Size |
Description |
byte |
1 byte |
Stores whole numbers from -128 to 127 |
short |
2 bytes |
Stores whole numbers from -32,768 to 32,767 |
int |
4 bytes |
Stores whole numbers from -2,147,483,648 to 2,147,483,647 |
long |
8 bytes |
Stores whole numbers from -9,223,372,036,854,775,808 to 9,223.372,036,854,775,808 |
float |
4 bytes |
Stores fractional numbers from 3.4e−038 to 3.4e+038. Sufficient for storing 6 to 7 decimal digits |
double |
8 bytes |
Stores fractional numbers from 1.7e−308 to 1.7e+038. Sufficient for storing 15 decimal digits |
boolean |
1 byte |
Stores true or false values |
char |
2 bytes |
Stores a single character/letter |
What is a variable?
Variables are containers for storing data values. Variable is a value that can change. Variable is a reserved space or a memory location to store some sort of information. Information can be of any data type such as int, string, boolean or float.
Java divides the operators into the following groups:
- Arithmetic operators : +, -, *, /,%, ++. —
- Assignment operators: =
- Relational operators: >, <, , >=, <=, ==,!=
- Logical operators: &&, ||, &, |, !
- Bitwise operators: & | ^ >> >>>
- Arithmetic Operators
What are Arithmetic Operators?
Arithmetic operators are used to perform common mathematical operations, such as:-
Operator |
Name |
Description |
Example |
+ |
Addition |
Adds together two values |
x + y |
– |
Subtraction |
Subtracts one value from another |
x – y |
* |
Multiplication |
Multiplies two values |
x * y |
/ |
Division |
Divides one value from another |
x / y |
% |
Modulus |
Returns the division remainder |
x % y |
++ |
Increment |
Increases the value of a variable by 1 |
++x |
— |
Decrement |
Decreases the value of |
–x |
Below is an example which show how these operators can be used.
public class Arithmetic_Operators {
public static void main(String[] args) {
int x = 45;
int y = 10;
int z;
z = x + y;
System.out.println("Addition of 2 numbers: " + z);
z = x - y;
System.out.println("Subtraction of 2 numbers: " + z);
z = x * y;
System.out.println("Multiplication of 2 numbers: " + z);
z = x / y;
System.out.println("Division of 1 number by another: " + z);
z = x % y;
System.out.println("Division Reminder: " + z);
x++;
System.out.println("Increment of x: " + x);
y--;
System.out.println("Decrement of y: " + y);
}
}
Output
Addition of 2 numbers: 55
Subtraction of 2 numbers: 35
Multiplication of 2 numbers: 450
Division of 1 number by another: 4
Division Reminder: 5
Increment of x: 46
Decrement of y: 9
What are Assignment Operators?
Assignment operators are use to assign values to variables.
Operator | Example | Same As |
= | x = 5 | x = 5 |
+= | x += 3 | x = x + 3 |
-= | x -= 3 | x = x – 3 |
*= | x *= 3 | x = x * 3 |
/= | x /= 3 | x = x / 3 |
%= | x %= 3 | x = x % 3 |
&= | x &= 3 | x = x & 3 |
|= | x |= 3 | x = x | 3 |
^= | x^= 3 | x = x ^ 3 |
>>= | x>>= 3 | x = x >> 3 |
<<= | x <<= 3 | x = x << 3 |
public class Assignment_Operator
public static void main(String[] args) {
int x = 45;
int y = 10;
int z = 15;
System.out.println("Value of x: " + x);
x += 5;
System.out.println("New value of x after addition: " + x);
// This will use new value of x which is 50 to perform the operation
x -= 10;
System.out.println("New value of x after substraction: " + x);
// This will use new value of x which is 40 to perform the operation
x *= 2;
System.out.println("New value of x after multiplication: " + x);
// This will use new value of x which is 80 to perform the operation
x /= 5;
System.out.println("New value of x after division: " + x);
// This will use new value of x which is 16 to perform the operation
x %= 3;
System.out.println("New value of x as division reminder: " + x);
// Multiple Assignment
System.out.println("Value of y and z: " + y + " and " + z);
y = z = 100;
System.out.println("New value of y and z: " + y + " and " + z);
}
}
Output
Value of x: 45
New value of x after addition: 50
New value of x after substraction: 40
New value of x after multiplication: 80
New value of x after division: 16
New value of x as division reminder: 1
Value of y and z: 10 and 15
New value of y and z: 100 and 100
What are Comparison Operators?
Comparison operators are use to compare two values.
Operator | Name | Example |
== | Equal to | x== y |
!= | Not equal | x != y |
> | Greater than | x > y |
< | Less than | x < y |
>= | Greater than or equal to | x >= y |
<= | Less than or equal to | x <= y |
public class Relational_Operator {
public static void main(String[] args) {
int x = 10;
int y = 20;
System.out.println("x is equal to y: " + (x == y));
System.out.println("x is not equal to y: " + (x != y));
System.out.println("x is greater than y: " + (x > y));
System.out.println("x is less than y: " + (x < y));
System.out.println("x is greater than or equal to y: " + (x >= y));
System.out.println("x is less than or equal to y: " + (x <= y));
}
}
Output
x is equal to y: false
x is not equal to y: true
x is greater than y: false
x is less than y: true
x is greater than or equal to y: false
x is less than or equal to y: true
What are Logical Operators?
Logical operators are use to determine the logic between variables or values.
Operator | Name | Description | Example |
&& | Logical and | Returns true if both statements are true | x < 5 && x < 10 |
|| | Logical or | Returns true if one of the statements is true | x < 5 || x < 4 |
! | Logical not | Reverse the result, returns false if the result is true | !(x < 5 && x < 10) |
public classLogical_Operators {
public static void main(String[] args) {
boolean value_1 = true;
boolean value_2 = false;
System.out.println("Check if both the boolean variables are true : " + (value_1 && value_2));
System.out.println("Check if even one of the boolean varibale is true : " + (value_1 || value_2));
System.out.println("Change the state of the Output_1 to false : " + (!value_1));
}
}
Output
Check if both the boolean variables are true : false
Check if even one of the boolean varibale is true : true
Change the state of the Output_1 to false : false
Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!
Great work Vibha!
LikeLike
This is very good and simple way to start learning on Java required for selenium prespective ..
LikeLike
Nice blog. Clears the basics of Java operators. I also want to know how to perform arithmetic operation on user input data?
LikeLike
Thanks Rahul. Below is the program for user input arithmetic operationimport java.util.Scanner;public class User_Input { public static void main(String[] args) { int x,y,z; Scanner input = new Scanner(System.in); System.out.println(\”Enter first number: \”); x=input.nextInt(); System.out.println(\”Enter second number:\”); y=input.nextInt(); z=x+y; System.out.println(\”Sum of 2 numbers is: \”+z); }}In this example, we have imported java.util package and scanner is a class in that package. Scanner class is used to get user input. nextInt() reads a int value from the user. I hope this has answered your question. Let me know if you have any more doubt
LikeLike
Thank you Yogi. I hope you keep on following my blogs for Selenium and Java
LikeLike