Java for Selenium Part 3


Java Data Types, Modifiers and Variables

i) Java Data Types

ii) Java Modifiers

iii) Java Variables
-------------------------------------------------------------
i) 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 program.

Ex: character, integer, float, boolean etc...

Java supports Explicit declaration of data types.

(We need to specify the data type before declaring Variables or Constants or Methods with return value.)

Syntax:

dataType variableName;
Or
dataType variableName = value;
Or
dataType variable1Name, variable2Name, variable3Name;
Or
dataType variable1Name=value; variable2Name=value, variable3Name=value;
-----------------------------------------
final dataType constantName = value;
--------------------------------------------
accessModifier dataType(int) methodName(int parameter1, int parameter2){
Statements
---------
----------
}
-------------------------------------------------
Two types of Data Types in Java,

a) Primitive Data Types

b) Non-Primitive Data Types/Reference Data types
--------------------------------------------------------------
a) Primitive Data Types (8 data types)

1) Integer Types

i) byte (8 bits)(-127 to 128)

ex:

byte a =10;

ii) short (16 bits)

short b = 1000;

iii) int (32 bits)

int c =1000000;

iv) long (64 bits)

long d =1000000000000000000;
---------------------------------------

2) Relational Types
(Numbers with decimal places)

v) float (32 bits)

float x =1.23;

vi) double (64 bits)

double y = 123.4567898;
-------------------------------------------
3) Characters

vii) char

char z ='A';
char e = '1';

d) Conditional

viii) boolean

boolean x = true/false;
-------------------------------------------------
b) Non-Primitive Data Types/Reference Data types
Non-Primitive Data Types or Reference Data types in Java are Objects and Arrays

Example:

Classname objectName = new ClassName();
----------------------------------------------------
Convert Data from one type to another.

Two types of Value Assignment for Variables

i) Initialization

int a=100; //Initialization

int b;
b=200; //Initialization

ii) Reading

1) Read using Input devices
2) Read from Files etc...

Note: If you read data (any type) then computer program considers the data as String type data,
if you to perform mathematical operations then we need to convert the data.

Note: We can't convert Alpha bytes to integers or float or double.
------------------------------------------------------------
ii) Modifiers in Java

Modifiers are keywords that we add to those definitions to change their meaning.

Two types of Modifiers in Java

a) Access Modifiers

b) Non Access Modifiers
---------------------------------------------------
a) Access Modifiers

We use access modifiers to define access control for classes, methods and variables.

1) public

public access modifier is accessible everywhere.

ex:

public class Sample {
}

2) private

The private access modifier is accessible only within class.

Ex:

private int a =123;

3) default

If we don't specify any modifier then it is treated as default, this can be accessible within package.

class Sample{
}

4) protected

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

Ex:

protected class Sample{
}
-----------------------------------------------------------
b) Non Access Modifiers

1) static

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

ex:

static int add(){
.
.
}

static int a =10;
---------------------------------------
2) final

final modifier for finalizing classes, methods and variables.

ex:

final int x =123;
.
.
x=234; //Incorrect
------------------------------
int x =123;
.
.
x=234; //Correct

3) abstract

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

ex:

abstract class Sample{
}
-------------------------------------------------------------------------
iii) 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 - HDD, DVD, USB drive etc...)
------------------------------------------------------
2) Declaring Variables

Java supports Explicit declaration of Data Types only.

Ex:

int a=10, b=20;
.
.
int res = a + b;//Correct
---------------------------
int a=10;
.
.
int res = a + b; //Incorrect
-----------------------------------
int a=10, b;
.
.
int res = a + b; //Incorrect
------------------------------------------
Example:

public static void main(String[] args) {
int a;
int b=100;

int c, d, e;
int f=1, g=2, h=4;
char k ='1';
char l ='A';
char m='a';
//char n ='ad';//Incorrect

double y =234.456789;

boolean z = true;

String myTool = "Selenium";
String t = "Test Automation using Selenium";
}
--------------------------------------------------------
3) Assign Values to Variables

a) Initialization

Ex:
int a=100;

b) Reading (Reading using Input devices, from files)

int a, b;
a=123;//Initialization
.
.
.
b=a;//Reading
---------------------------------------------
4) Variables Naming Restrictions
a) Java Variables are Case sensitive.

int a;
int B;
a=100;
b=200;//incorrect
---------------------------------------
VBScript

Dim a
A =100
Msgbox a //Correct
---------------------------------------------
b) Java variable names should start with a letter or $ or _

ex:
myvar
MYVAR
$abc
_xyz
myvar7
7myvar //Incorrect
*myvar //Incorrect
--------------------------------------------------------
c) Variable names should not match with Java keywords / Reserved word

int a; //Correct
int new; Incorrect
-----------------------------------------------------------
d) Must be unique in the scope of declaration

e) Must not exceed 255 characters
-----------------------------------------------------------------
5) Types of Variables
a) Local Variables
Local Variables are declared in methods or blocks (Conditions, Loops etc...)

b) Instance variables
Instance variables are declared in a class but outside of a method or any block

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

> The static variable gets memory only once in a class area at the time of class loading.

It makes our program efficient (i.e it saves memory)
--------------------------------------------------------------
Example:

public int salary(){
int mySalary = 10000 + 2000 + 1500 + a;
return mySalary;
}

public static void main(String[] args) {
int b=20;
System.out.println(a);
System.out.println(b);

if (b > a){
int x=123;
System.out.println("B is a Big Number");
System.out.println(a); //Static variable
System.out.println(b); //Instance Variable
System.out.println(x); //Local Variable
}
//System.out.println(x);//Error
System.out.println(Class1.a);
System.out.println(a);

}
}
-----------------------------------------------------------

Java for Selenium Part 4 Link

0 comments:

Post a Comment