i) Java Basics for Selenium ( Variable, Data Types & Objects )
Variable:
A Java variable is a piece of memory that can contain a data value during java program execution.
Variable Declaration:
To declare a variable, you must specify the data type & give the variable a unique name.
Ex., int a,b,c;
float pi;
double d;
char a;
Variable Initialization:
To initialize a variable, you must assign it a valid value.
Ex., int a=2,b=4,c=6;
float pi=3.14f;
double do=20.22d;
char a=’v’;
Types of variables
In Java, there are three types of variables:
Local Variables
Instance Variables
Static Variables
1) Local Variables
Local Variables are a variable that are declared inside the body of a method.
2) Instance Variables
Instance variables are defined without the STATIC keyword .They are defined Outside a method declaration. They are Object specific and are known as instance variables.
3) Static Variables
Static variables are initialized only once, at the start of the program execution. These variables should be initialized first, before the initialization of any instance variables.
Example,
class Testdemo{
static int a = 1; //static variable
int data = 99; //instance variable
void method() {
int b = 90; //local variable
}
}
What is Data Types in Java?
Data Types in Java are defined as specifiers that allocate different sizes and types of values that can be stored in the variable or an identifier. Data types in Java can be divided into two parts :
Primitive Data Types :- which include integer, character, boolean, and float
Non-primitive Data Types :- which include classes, arrays and interfaces.
There are eight primitive data types:
byte (1 byte)
short (2 bytes)
int (4 bytes)
long (8 bytes)
float (4 bytes)
double (8 bytes)
char (2 bytes)
boolean (1 byte)
What is Class in Java?
CLASS are a set of instructions to build a specific type of object. Class in Java determines how an object will behave and what the object will contain.
Syntax:
class <class_name>{
field;
method;
}
What is Object in Java?
OBJECT is an instance of a class.
Syntax:
ClassName ReferenceVariable = new ClassName();
Comments
Post a Comment