8 – Java Variables

Selenium Class 8 – Java Variables

Yesterday Class Continuation:

Java Data Types
2) Non Primitive data types / Reference data types

Non Primitive data types in Java are Objects (String, Array etc…)
Example:
String tool = “Selenium”
………………………….
123 – Integer Data Type
‘Y’ -Character
123.34 – float/double
“Selenium Testing” – String
“abc123*&^” – String
“123” – String
true – boolean
1 – Integer
‘1’ – Character
………………………….
How to handle Date type data? –
Converting Data / Data Conversion
Converting data from one type to another…

Assigning Values to Variables
1) Initialization – No Data Conversation
2) Reading – (Data Conversation is required)
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…

Java Variables

1) 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…)

Note: Variables store in primar memory (RAM)

2) Declaration of variables

Java supports Explicit declaration of variables

In Java:
dataType VariableName;
Ex:
int num;

dataType VariableName=value;

dataType Variable1Name, Variable2Name, Variable3Name;

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

int a=100;

long y=9098787879l

char b=’D’;

float x=1.234f

double c=10.234;

String d = “Java Programming”;

boolean z=true;

3) Assign values to variables

a) Initialization
int a=10;
or
int b;
b=20;

b) Reading
a) Read input using input devices
b) Read data from files (text, excel, database file….)
c) Read data using Application objects

int a=100; //Initialization
int b=a; //Reading

int a, b, c;
int d, b, f;//Incorrect

int a=100, b=100, c=100;

Assign
initialization
Reading

4) Variable naming restrictions

a) Java Variables are case sensitive

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

b) Java variable name 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

if, for, import, new…
true, false, null….

int a; //correct
int while; //incorrect

d) Java variables Must be unique in the scope of declaration

Note:
1) we can declare Instance variables inside main method or outside method If you declare inside then use it inside only, outside, outside only

2) We have to declare static variables oust side the main method only and scope is for entire class(Interface section and main program), and we can’t declare static variables inside the main method…
……………………………………………………
Class{
Interface Section

Static variable – Entire Class (Interface section and Main Program)
Instance variable1 (only Interface section)
block 1{
local varaible1
Static variable
Instance variable1 //Local to block 1 only
}

block 2{
local varaible2
}

block 3{
local varaible3
}

Main Program {
Instance variable2 //(Main program only)

block 1{
local varaible1
Instance variable2
Static variable
}

block 2{
local varaible2
Instance variable2
Static variable
}

block 3{
local varaible3
Instance variable2
}

}
}
……………………………………………………
Scenario 1

Class {
Interface section…
Main{
}
}

Scenario 2

Class {
Main{
}
Interface section…
}
……………………………………………………
Scenario 3

Class {
Interface section…
Main{
}
Interface section…
}
……………………………………………………
Interface section is outside of the main method…

Class {
Interface section…
method 1
Main{
main program statemet 1 – executes
method 1 – executes
method 2 – executes
}
Interface section…
method 2
}
……………………………………………………
Explicit Declaration of Variables:

In Java
int a;
a=100;//Explicit variable
b=200;//Incorrect – Implicit variable

In VBScript
Dim a
a=100 ‘Correct – Explicit variable
b=200 ‘Correct – Implicit variable
……………………………………………………
Java Program for Variable Declaration:

package abcd;

public class Comments {
// Interface Section Part-1
static int x=234; //Static Variable/Class Variable
int z=123; // Instance variable (Outside the main method)

public int add1(){
int a=10, b=20, c=30;//Local variables
return a+b+x+z;
}
//System.out.println(“Hello Java”);
public static void main(String[] args) {
System.out.println(“Hello Java”);

int a=123, b=345, c=400;//Instance variables (inside the main method)

int y=21; //Instance variable (inside the main method)

System.out.println(x+y);
if (a>b){
System.out.println(“A is a big number”);
}
else {
System.out.println(“B is a big number”);
}

if (c>b){
System.out.println(“A is a big number”);
}
else {
System.out.println(“B is 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”);
}

Comments obj = new Comments();
int res = obj.add1();
System.out.println(res);//30

int res2 = obj.add2();
System.out.println(res2);//302

}
// Interface Section Part-2
public int add2(){
int a=101, b=201;// Local Variables
return a+b+x+z;
}
}