Java Tutorial 2
(Comments inwards Java, Java Data Types, Java Modifiers, Java Variables as well as Operators inwards Java)
I) Comments inwards Java
Comments are English linguistic communication words, tin endure used for code documentation.
Purpose of Comments:
a) To brand the code Readable
b) To brand the code disable shape execution
Comments Syntax inwards Java:
Use // for unmarried job Comment or partial job equally comment
Use /* statements
----------
-----------
----------
*/ for multiple lines comment
Or
In Eclipse
Select statements
Source bill of fare -> Add Block Comment
Uncomment:
Select Comment block
Source bill of fare -> Remove comment block
-------------------------------------
Usage of Comments inwards Test Automation
a) To write Test instance headers
b) To write Method headers
c) To explicate complex logic
d) To explicate resources usage
------------------------------
Example:
public degree Comments {
world static void principal (String [] args){
int a = 10, b, c; // Declaration of Variables
// It is a Sample Program
System.out.println(a);// 10
b = 50;
c = 2;
/*if (a > b) {
System.out.println("A is a Big Number");
}
else {
System.out.println("B is a Big Number");
}*/
}
}
---------------------------------------
II) Java Data Types
Data type is a classification of the type of information that a variable or object tin concord inwards Computer programming.
Example:
Character, Integer, String, float etc...
Java supports explicit annunciation of Data types.
(We need to specify the information type earlier annunciation of variables.)
Syntax:
dataType VariableName
Example:
int a;
or
int a = 100;
-------------------------------------
In Java nosotros accept 2 types of Data types.
i) Primitive Data types
ii) Non -primitive Data types
------------------------------
i) Primitive Data types (8 information types)
Integer Data types
---------------------
1) byte (8 bits)
Example:
byte a = 10;
2) brusque (16 bits)
Ex:
short b = 10000;
3) int (32 bits)
int c = 100000;
4) long (64 bits)
Ex:
long d = 100000L
-----------------------------
Rational Data types (Numbers alongside decimal places)
5) float (32 bits)
Ex:
float a = 1.2;
6) double (64 bits)
Ex:
double b = 19.234567;
----------
Characters
7) char (16 bits)
Ex:
char a = 'Z'
-------------------
Conditional
8) Boolean
Ex:
boolean a = true;
--------------------------------
ii) Non -primitive Data types
Non -primitive Data types inwards Java are objects as well as Arrays.
Ex:
Button = novel Button("OK")
--------------------------------------------
III) Java Modifiers
Modifiers are keywords that nosotros add together to those definitions to modify their meanings.
Two types of modifiers inwards Java:
i) Access Modifiers
ii) Non Access Modifiers
-------------------------------
i) Access Modifiers
We purpose Access modifiers to define Access command for classes, methods as well as variables.
There are four types of Access modifiers inwards Java
1) Private
The somebody access modifier is accessible alone within class.
Ex:
class Abc {
private int a = 100;
.
.
.
}
--------------------------
2) default
If nosotros don't purpose whatsoever modifier as well as then it is treated equally default, this tin endure accessible alone within package.
Ex:
class Sample{
int a = 10;
.
.
}
-----------------------------------
3) protected
The protected access modifier is accessible within packet as well as out side of the packet but
through Inheritance only.
-----------------------------
4) world
public access modifier is accessible everywhere.
Ex:
public degree Abc{
.
.
}
--------------------------------------------------
Modifier Within class within package exterior of the package(by sub class) Outside of the package
----------------------------------------------------------------------------------------
private Y N N N
------------------------------------------------------------------------------------
default Y Y N N
-----------------------------------------------------------------------------------
protected Y Y Y N
----------------------------------------------------------------------------------
public Y Y Y Y
---------------------------------------------------------------------------------
ii) Non Access modifiers
1) static
static modifiers are used to practice degree variable as well as degree methods which tin endure accessed without instance of a class.
Ex:
class H5N1 {
static String elevate = "Selenium";
}
---------------------
2 final
final modifier for finalizing the implementation of classes, methods as well as variables.
class H5N1 {
int a = 10;
final int b = 20;
.
.
a = 30;
b = 50;//Incorrect
}
--------------------
3) abstract
abstract modifier is to practice abstract classes as well as abstract methods.
Ex:
abstract sample {
Statements
--------
---------
------
}
------------------
4) synchronized
--------------------------------
IV) Variables inwards Java
1) What is Variable?
A named retentivity location to shop or concord the data.
Two types of retentivity inwards figurer environment:
i) Primary retentivity - RAM
ii) Secondary retentivity -ROM
Ex: CD, DVD, HDD, USB crusade etc...
---------------------
2) Declaration of Variables
Syntax:
dataType variableName;
Ex:
int a;
-----------------
dataType varaible1, variable2, varaible3;
ex:
int a, b, c;
----------------------
dataType variableName = value;
Ex:
int a = 100;
--------------------------
3) Assigning values to variables
i) Initialization
Ex:
int a;
a = 100;
ii) Reading
using input devices
from files (text, excel)
from databases
from Application objects
--------------------------------
4) Types of Variables
In Java nosotros accept iii types of variables.
i) Instance variables
A variable that is declared within the degree but exterior the method.
ii) Local variables
A variable that is declared within the Method.
iii) Static / degree variables
A variable that is declared equally static, It cannot endure local.
------------------------------
5) Naming Restrictions
i) Java variables are instance sensitive, monkey is non the equally MONKEY or Monkey.
ii) Java variable elevate must origin alongside a alphabetic quality or $ or _
Embedded periods can't endure used.
Example:
myvar
MYVAR
$myvar
_myvar
myvar1
myvar_1
-----------
my var
my.var
1myvar
*myvar
my-var
-----------------------
iii) Variable names cannot endure equal to Java reserved words.
Ex:
int
for
import
-------------------
iv) Must endure unique inwards the reach of declaration.
--------------------------------------------
Variables example:
public static void principal (String [] args){
// Variable Declaration
int a;
a = 10; // Initialization
// Declaration of multiple variables inwards a statement
int b, c, d;
b = 20;
c = 30;
d = 40;
// Declaration of multiple variable as well as Assigning values.
int e = 50, f=60, g = 70;
char x ='A';
double y = 2.345;
String z = "Selenium123";
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println(e);
System.out.println(f);
System.out.println(g);
System.out.println(x);
System.out.println(y);
System.out.println(z);
}
}
-----------------------------------------------
V) Java Operators
Operators are used to perform mathematical, Comparison as well as Logical operations.
Important categories of Operators:
i) Assignment Operators
ii) Arithmetic operators
iii) Relational operators
iv) Logical Operators
etc...
-------------------------------------
i) Assignment Operators
1) Assignment operator =
a = 10;
2) Add as well as Assign Operator +=
a = 10;
a += 20;
3) Subtract as well as Assign -=
a = 10;
a -= 5;
4) Multiply as well as Assign *=
a = 10
a *= 5;
--------------------------------------
Example:
public static void principal (String [] args){
int a = 10;
System.out.println(a);// 10
a += 10;
System.out.println(a);//20
a -= 10;
System.out.println(a);//10
a *= 5;
System.out.println(a);//50
}
}
--------------------------------------
ii) Arithmetic Operators
Arithmetic Operators supply value based Result.
1) Addition + (for Addition as well as String concatenation)
2) Subtraction - (for Subtraction as well as negation)
3) Multiplication *
4) Division /
5) Modules %
6) Increment ++
7) Decrement --
-------------------------------
Example:
public static void principal (String [] args){
int a = 10, b = 5;
String c = "Selenium", d = " Testing";
System.out.println("Addition of a, b is: " + (a + b)); // Addition of a, b is: 15
System.out.println("Concatenation of c, d is: " + (c+d)); //Concatenation of c, d is: Selenium Testing
System.out.println("Subtraction of a, b is: " + (a-b)); // 5
System.out.println("Multiplication of a, b is: " + (a*b)); // 50
System.out.println("Divison of a, b is: " + (a/b)); //2
System.out.println("Modulas of a, b is: " + (a%b)); //0
a = ++b;
System.out.println(a); //6
b = 5;
a = --b;
System.out.println(a);//4
b =5;
a = b+4;
System.out.println(a); //9
b = 5;
a = b-4;
System.out.println(a);//1
}
}
---------------------------------------
iii) Relational Operators
Relational operators supply boolean or logical upshot (true or false)
1) ==
2) !=
3) >
4) >=
5) <
6) <=
------------------------------
Example:
public degree OperatorsExample {
public static void principal (String [] args){
int a = 10, b = 20;
System.out.println("a > b is " + (a>b)); //False
System.out.println("a >= b is " + (a >= b)); //False
System.out.println("a < b is " + (a<b)); //True
System.out.println("a <= b is " + (a <= b)); //True
System.out.println("a == b is " + (a == b)); //False
System.out.println("a != b is " + (a != b)); //True
}
}
------------------------------------------
iv) Logical Operators
1) Logical Not operator !
2) Logical And Operator &&
3) Logical Or operator ||
--------------------------------
Result Criteria for Not operator
Operand 1 Operand 2 Result
----------------------------------------
true true false
true false true
false true true
false false true
-----------------------------------------
Result Criteria for Add operator
Operand 1 Operand 2 Result
----------------------------------------
true true true
true false false
false true false
false false false
-----------------------------------------
Result Criteria for Or operator
Operand 1 Operand 2 Result
----------------------------------------
true true true
true false true
false true true
false false false
-----------------------------------------
Example 1:
public degree OperatorsExample {
public static void principal (String [] args){
boolean a = true, b = false;
System.out.println(! (a && b)); // true
System.out.println((a && b)); //false
System.out.println((a || b));//true
}
}
----------------------------------
Example 2:
public degree OperatorsExample {
public static void principal (String [] args){
int a = 100, b = 500, c = 700;
if ((a > b) && (a > c)) {
System.out.println("A is a Big Number");
}
else
{
System.out.println("A is Not a Big Number");
}
}
}
-----------------------------------------
public static void principal (String [] args){
int a = 100, b = 500, c = 700;
if (!(a > b) && !(a > c)) {
System.out.println("A is a Big Number");
}
else
{
System.out.println("A is Not a Big Number");
}
}
---------------------------
public static void principal (String [] args){
int a = 100, b = 50;
if (! (a > b)) {
System.out.println("A is a Big Number");
}
else
{
System.out.println("B is a Big Number");
}
}
------------------------------------------------
Java Flow Control statements
Conditional statements
Loop statements
-------------------------------------
VI) Java Conditional Statements
a) Usage of Conditional statements inwards Test Automation:
i) To insert verification points
ii) Error handling
--------------
b) Two types of conditional statements inwards Java
i) if statement
ii) Switch statement
-----------------------------
c) Types of Conditions
i) Single condition
Ex:
if (a > b) {
Statements
-------
--------
}
ii) Compound Condition
Ex:
if ((a > b) && (a < c)) {
Statements
-------
--------
}
iii) Nested condition
Ex:
if (a > b) {}
if (a > c) {}
if (a > d) {
Statements
---------
----------
}
-------------------------------------
d) Usage of Conditional Statements
(Comments inwards Java, Java Data Types, Java Modifiers, Java Variables as well as Operators inwards Java)
I) Comments inwards Java
Comments are English linguistic communication words, tin endure used for code documentation.
Purpose of Comments:
a) To brand the code Readable
b) To brand the code disable shape execution
Comments Syntax inwards Java:
Use // for unmarried job Comment or partial job equally comment
Use /* statements
----------
-----------
----------
*/ for multiple lines comment
Or
In Eclipse
Select statements
Source bill of fare -> Add Block Comment
Uncomment:
Select Comment block
Source bill of fare -> Remove comment block
-------------------------------------
Usage of Comments inwards Test Automation
a) To write Test instance headers
b) To write Method headers
c) To explicate complex logic
d) To explicate resources usage
------------------------------
Example:
public degree Comments {
world static void principal (String [] args){
int a = 10, b, c; // Declaration of Variables
// It is a Sample Program
System.out.println(a);// 10
b = 50;
c = 2;
/*if (a > b) {
System.out.println("A is a Big Number");
}
else {
System.out.println("B is a Big Number");
}*/
}
}
---------------------------------------
II) Java Data Types
Data type is a classification of the type of information that a variable or object tin concord inwards Computer programming.
Example:
Character, Integer, String, float etc...
Java supports explicit annunciation of Data types.
(We need to specify the information type earlier annunciation of variables.)
Syntax:
dataType VariableName
Example:
int a;
or
int a = 100;
-------------------------------------
In Java nosotros accept 2 types of Data types.
i) Primitive Data types
ii) Non -primitive Data types
------------------------------
i) Primitive Data types (8 information types)
Integer Data types
---------------------
1) byte (8 bits)
Example:
byte a = 10;
2) brusque (16 bits)
Ex:
short b = 10000;
3) int (32 bits)
int c = 100000;
4) long (64 bits)
Ex:
long d = 100000L
-----------------------------
Rational Data types (Numbers alongside decimal places)
5) float (32 bits)
Ex:
float a = 1.2;
6) double (64 bits)
Ex:
double b = 19.234567;
----------
Characters
7) char (16 bits)
Ex:
char a = 'Z'
-------------------
Conditional
8) Boolean
Ex:
boolean a = true;
--------------------------------
ii) Non -primitive Data types
Non -primitive Data types inwards Java are objects as well as Arrays.
Ex:
Button = novel Button("OK")
--------------------------------------------
III) Java Modifiers
Modifiers are keywords that nosotros add together to those definitions to modify their meanings.
Two types of modifiers inwards Java:
i) Access Modifiers
ii) Non Access Modifiers
-------------------------------
i) Access Modifiers
We purpose Access modifiers to define Access command for classes, methods as well as variables.
There are four types of Access modifiers inwards Java
1) Private
The somebody access modifier is accessible alone within class.
Ex:
class Abc {
private int a = 100;
.
.
.
}
--------------------------
2) default
If nosotros don't purpose whatsoever modifier as well as then it is treated equally default, this tin endure accessible alone within package.
Ex:
class Sample{
int a = 10;
.
.
}
-----------------------------------
3) protected
The protected access modifier is accessible within packet as well as out side of the packet but
through Inheritance only.
-----------------------------
4) world
public access modifier is accessible everywhere.
Ex:
public degree Abc{
.
.
}
--------------------------------------------------
Modifier Within class within package exterior of the package(by sub class) Outside of the package
----------------------------------------------------------------------------------------
private Y N N N
------------------------------------------------------------------------------------
default Y Y N N
-----------------------------------------------------------------------------------
protected Y Y Y N
----------------------------------------------------------------------------------
public Y Y Y Y
---------------------------------------------------------------------------------
ii) Non Access modifiers
1) static
static modifiers are used to practice degree variable as well as degree methods which tin endure accessed without instance of a class.
Ex:
class H5N1 {
static String elevate = "Selenium";
}
---------------------
2 final
final modifier for finalizing the implementation of classes, methods as well as variables.
class H5N1 {
int a = 10;
final int b = 20;
.
.
a = 30;
b = 50;//Incorrect
}
--------------------
3) abstract
abstract modifier is to practice abstract classes as well as abstract methods.
Ex:
abstract sample {
Statements
--------
---------
------
}
------------------
4) synchronized
--------------------------------
IV) Variables inwards Java
1) What is Variable?
A named retentivity location to shop or concord the data.
Two types of retentivity inwards figurer environment:
i) Primary retentivity - RAM
ii) Secondary retentivity -ROM
Ex: CD, DVD, HDD, USB crusade etc...
---------------------
2) Declaration of Variables
Syntax:
dataType variableName;
Ex:
int a;
-----------------
dataType varaible1, variable2, varaible3;
ex:
int a, b, c;
----------------------
dataType variableName = value;
Ex:
int a = 100;
--------------------------
3) Assigning values to variables
i) Initialization
Ex:
int a;
a = 100;
ii) Reading
using input devices
from files (text, excel)
from databases
from Application objects
--------------------------------
4) Types of Variables
In Java nosotros accept iii types of variables.
i) Instance variables
A variable that is declared within the degree but exterior the method.
ii) Local variables
A variable that is declared within the Method.
iii) Static / degree variables
A variable that is declared equally static, It cannot endure local.
------------------------------
5) Naming Restrictions
i) Java variables are instance sensitive, monkey is non the equally MONKEY or Monkey.
ii) Java variable elevate must origin alongside a alphabetic quality or $ or _
Embedded periods can't endure used.
Example:
myvar
MYVAR
$myvar
_myvar
myvar1
myvar_1
-----------
my var
my.var
1myvar
*myvar
my-var
-----------------------
iii) Variable names cannot endure equal to Java reserved words.
Ex:
int
for
import
-------------------
iv) Must endure unique inwards the reach of declaration.
--------------------------------------------
Variables example:
public static void principal (String [] args){
// Variable Declaration
int a;
a = 10; // Initialization
// Declaration of multiple variables inwards a statement
int b, c, d;
b = 20;
c = 30;
d = 40;
// Declaration of multiple variable as well as Assigning values.
int e = 50, f=60, g = 70;
char x ='A';
double y = 2.345;
String z = "Selenium123";
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println(e);
System.out.println(f);
System.out.println(g);
System.out.println(x);
System.out.println(y);
System.out.println(z);
}
}
-----------------------------------------------
V) Java Operators
Operators are used to perform mathematical, Comparison as well as Logical operations.
Important categories of Operators:
i) Assignment Operators
ii) Arithmetic operators
iii) Relational operators
iv) Logical Operators
etc...
-------------------------------------
i) Assignment Operators
1) Assignment operator =
a = 10;
2) Add as well as Assign Operator +=
a = 10;
a += 20;
3) Subtract as well as Assign -=
a = 10;
a -= 5;
4) Multiply as well as Assign *=
a = 10
a *= 5;
--------------------------------------
Example:
public static void principal (String [] args){
int a = 10;
System.out.println(a);// 10
a += 10;
System.out.println(a);//20
a -= 10;
System.out.println(a);//10
a *= 5;
System.out.println(a);//50
}
}
--------------------------------------
ii) Arithmetic Operators
Arithmetic Operators supply value based Result.
1) Addition + (for Addition as well as String concatenation)
2) Subtraction - (for Subtraction as well as negation)
3) Multiplication *
4) Division /
5) Modules %
6) Increment ++
7) Decrement --
-------------------------------
Example:
public static void principal (String [] args){
int a = 10, b = 5;
String c = "Selenium", d = " Testing";
System.out.println("Addition of a, b is: " + (a + b)); // Addition of a, b is: 15
System.out.println("Concatenation of c, d is: " + (c+d)); //Concatenation of c, d is: Selenium Testing
System.out.println("Subtraction of a, b is: " + (a-b)); // 5
System.out.println("Multiplication of a, b is: " + (a*b)); // 50
System.out.println("Divison of a, b is: " + (a/b)); //2
System.out.println("Modulas of a, b is: " + (a%b)); //0
a = ++b;
System.out.println(a); //6
b = 5;
a = --b;
System.out.println(a);//4
b =5;
a = b+4;
System.out.println(a); //9
b = 5;
a = b-4;
System.out.println(a);//1
}
}
---------------------------------------
iii) Relational Operators
Relational operators supply boolean or logical upshot (true or false)
1) ==
2) !=
3) >
4) >=
5) <
6) <=
------------------------------
Example:
public degree OperatorsExample {
public static void principal (String [] args){
int a = 10, b = 20;
System.out.println("a > b is " + (a>b)); //False
System.out.println("a >= b is " + (a >= b)); //False
System.out.println("a < b is " + (a<b)); //True
System.out.println("a <= b is " + (a <= b)); //True
System.out.println("a == b is " + (a == b)); //False
System.out.println("a != b is " + (a != b)); //True
}
}
------------------------------------------
iv) Logical Operators
1) Logical Not operator !
2) Logical And Operator &&
3) Logical Or operator ||
--------------------------------
Result Criteria for Not operator
Operand 1 Operand 2 Result
----------------------------------------
true true false
true false true
false true true
false false true
-----------------------------------------
Result Criteria for Add operator
Operand 1 Operand 2 Result
----------------------------------------
true true true
true false false
false true false
false false false
-----------------------------------------
Result Criteria for Or operator
Operand 1 Operand 2 Result
----------------------------------------
true true true
true false true
false true true
false false false
-----------------------------------------
Example 1:
public degree OperatorsExample {
public static void principal (String [] args){
boolean a = true, b = false;
System.out.println(! (a && b)); // true
System.out.println((a && b)); //false
System.out.println((a || b));//true
}
}
----------------------------------
Example 2:
public degree OperatorsExample {
public static void principal (String [] args){
int a = 100, b = 500, c = 700;
if ((a > b) && (a > c)) {
System.out.println("A is a Big Number");
}
else
{
System.out.println("A is Not a Big Number");
}
}
}
-----------------------------------------
public static void principal (String [] args){
int a = 100, b = 500, c = 700;
if (!(a > b) && !(a > c)) {
System.out.println("A is a Big Number");
}
else
{
System.out.println("A is Not a Big Number");
}
}
---------------------------
public static void principal (String [] args){
int a = 100, b = 50;
if (! (a > b)) {
System.out.println("A is a Big Number");
}
else
{
System.out.println("B is a Big Number");
}
}
------------------------------------------------
Java Flow Control statements
Conditional statements
Loop statements
-------------------------------------
VI) Java Conditional Statements
a) Usage of Conditional statements inwards Test Automation:
i) To insert verification points
ii) Error handling
--------------
b) Two types of conditional statements inwards Java
i) if statement
ii) Switch statement
-----------------------------
c) Types of Conditions
i) Single condition
Ex:
if (a > b) {
Statements
-------
--------
}
ii) Compound Condition
Ex:
if ((a > b) && (a < c)) {
Statements
-------
--------
}
iii) Nested condition
Ex:
if (a > b) {}
if (a > c) {}
if (a > d) {
Statements
---------
----------
}
-------------------------------------
d) Usage of Conditional Statements