Java for Selenium Part 3


Java Data Types, Modifiers as well as 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 information that a Variable or Constant or Method tin plow over the sack concord inwards reckoner program.

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

Java supports Explicit proclamation of information types.

(We ask to specify the information type earlier declaring Variables or Constants or Methods amongst supply 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 inwards Java,

a) Primitive Data Types

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

1) Integer Types

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

ex:

byte a =10;

ii) curt (16 bits)

short b = 1000;

iii) int (32 bits)

int c =1000000;

iv) long (64 bits)

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

2) Relational Types
(Numbers amongst 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 inwards Java are Objects as well as Arrays

Example:

Classname objectName = novel ClassName();
----------------------------------------------------
Convert Data from i 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 lot read information (any type) as well as thus reckoner programme considers the information equally String type data,
if you lot to perform mathematical operations as well as thus nosotros ask to convert the data.

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

Modifiers are keywords that nosotros add together to those definitions to alter their meaning.

Two types of Modifiers inwards Java

a) Access Modifiers

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

We role access modifiers to define access command for classes, methods as well as variables.

1) public

public access modifier is accessible everywhere.

ex:

public flat Sample {
}

2) private

The someone access modifier is accessible solely inside class.

Ex:

private int a =123;

3) default

If nosotros don't specify whatever modifier as well as thus it is treated equally default, this tin plow over the sack hold upward accessible inside package.

class Sample{
}

4) protected

The protected access modifier is accessible inside package, exterior of the packet too but through Inheritance.

Ex:

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

1) static

static modifier is used to create classes, methods as well as variables.

ex:

static int add(){
.
.
}

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

final modifier for finalizing classes, methods as well as 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 as well as abstract methods.

ex:

abstract flat Sample{
}
-------------------------------------------------------------------------
iii) Java Variables

1) What is Variable?

A named retention location to shop the temporary information inside a program.

Two types of Memory inwards Computer environment,

a) Primary retention (RAM)

b) Secondary retention (ROM - HDD, DVD, USB elbow grease etc...)
------------------------------------------------------
2) Declaring Variables

Java supports Explicit proclamation 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 foremost amongst a alphabetic quality or $ or _

ex:
myvar
MYVAR
$abc
_xyz
myvar7
7myvar //Incorrect
*myvar //Incorrect
--------------------------------------------------------
c) Variable names should non tally amongst Java keywords / Reserved word

int a; //Correct
int new; Incorrect
-----------------------------------------------------------
d) Must hold upward unique inwards the reach of declaration

e) Must non overstep 255 characters
-----------------------------------------------------------------
5) Types of Variables
a) Local Variables
Local Variables are declared inwards methods or blocks (Conditions, Loops etc...)

b) Instance variables
Instance variables are declared inwards a flat but exterior of a method or whatever block

c) Static / Class Variables
Static variables are declared equally static, these can't hold upward local.

> The static variable gets retention solely i time inwards a flat surface area at the fourth dimension of flat loading.

It makes our programme 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 iv Link

Sumber http://www.gcreddy.com/
Post a Comment (0)
Previous Post Next Post