Data Types and Variables

What is Data Type?

data type indicates what sort of value or the type of data the variable can represents, such as integer,floating-point numbers, character, boolean or an alphanumeric string.
There are other data types as well like shortlong and float but in Selenium programming you may not face any situation where you have to you these data types. These data types are used when each byte of memory is important for better performance of the system.

What is Variable?

As it names suggest, variable is a value that can change. A simple computer program uses a set of instructions and data. Data can be constants or fixed values that never change and it can be variable that can change during the execution. Rather than entering data directly into a program, a programmer can use variables to represent the data. When compiler run the program, variables used in the instructions are replaced with the real data entered by the programmer.

In technical term, variable is a reserved space or a memory location to store some sort of information. Information can be of any data type such as intstringbool or float. Each variable in the program has its space allocated in the memory and based on the data type, system allocates only required memory to the variables.
Every variable in the program has it’s name and data type.

How Variables work?

Declaring Variables: Ask compiler to allocate some amount of memory to the variable for storing some value. As compiler would like to know the data type of the variable, so that it can allocate only required memory to the variable. That can be done by ‘declaring the data type’ of the variable.
Naming Variables: After variable declaration, when the program needs the stored value, compiler will go to the memory location to fetch the value and pass it back to the program. To effectively handle this transaction, the compiler would need two pieces of information from the program: name and data typeof the variable. Give the name of your choice to the variable and let the compiler know about it. So that next time when you refer that name, compiler would know what piece of memory you are refereeing to. That name of the variable is called the Identifier.
This process is called as Variable Declaration.
DataTypeOfVariable     VariableName;
Initialization of Variables: Once variable declaration is done, then a value can be stored in the reserved memory location. Before that you would not be able to use the variable. Initialization of a variable is very important before using the variable for any sort of operations. Putting an initial value is referred to asInitializing the Variable.
VariableName = Value;

Examples of different data types

Data Type – boolean

Boolean data type is use to store only ‘boolean‘ values such as ‘true‘ and ‘false‘. To declare a Boolean variable, you can use the ‘boolean‘ keyword.
Here is an example:
Above test will produce this:
Test Result is: true
Test Result is: false


Data Type – int

Integer data type is use to store numeric/numbers in the variable. In technical term, an integer store 32- bit of information and it stores the value between -2,147,483,648 and 2,147,484,647. But a decimal number can not be store in the ‘int‘ data type.
Here is an example:
Above test will produce this:
Car is running at the speed of: 20
Current speed of the car is: 40


Data Type – char

Character data type is use to store just one character in the variable. It is very rarely used in Selenium. To initialize such a variable, assign a character with in the single-quoted
Here is an example:
Above test will produce this:
Value of char is : P


Data Type – double

To declare a variable used to hold such a decimal number, you can use the double keyword.
Here is an example:
Above test will produce this:
PI: 3.14159

 Notes: Rules for naming a variable
  1. A variable name can be of single letter of alphabets (A-Z).
  2. Variable name can start only with letter, under score ‘_’ and the dollar sign ‘$’.
  3. Variable name can not start with digit.
  4. Variable name can include digits.
  5. The name of a variable cannot be one of the words that the Java languages has reserved for its own use such as static, this, void, final, for etc.

Every body follow their own pattern to declare variables and I follow my own. I usually declare all thestring variables with small letter ‘s’ and int variable with small letter ‘i’. So that in my program when I see any variable starts with letter ‘s‘, I can understand that it is a String variable. For example:
String sName = “ToolsQATraining”;
int iNum = 10;
bollean bResult = true;

Example of declaring multiple variables: