Strings as well as Arrays inwards Java
i) String Handling inwards Java
ii) Arrays inwards Java
-------------------------------
i) String Handling inwards Java
a) What is String?
> String is a sequence of characters written double quotes.
------------
Numbers
-------------
Integers - byte, short, int, long information types
Floating betoken values/decimal values- float, double information types
-----------------------------
Character -char
------------------
Logical values
boolean
----------------------
String -Object
----------------
> String may bring Alphabets, Numbers as well as Special characters.
Example:
System.out.println("Selenium");//Selenium
System.out.println("123Selenium");//123Selenium
System.out.println("Selenium*&123");//Selenium*&123
System.out.println("1234");//1234
--------------------------------
b) Create Strings
String myTool ="Selenium";//String Variable
String [] myTools ={"UFT", "Selenium", "LoadRunner", "RFT"}; //Array of Strings
System.out.println(myTool);//Selenium
for (String tool: myTools){
System.out.println(tool);
}
------------------------------------
c) Operations on Strings
1) Concatenating Strings
String str1 = "Selenium ";
String str2 ="Testing";
System.out.println(str1 + str2);//Selenium Testing
System.out.println("Selenium" + (1 + 1));//Selenium2
System.out.println("Selenium" + 1 + 1);//Selenium11
System.out.println(1 + 1 + "Selenium");//2Selenium
System.out.println("1" + 1 + "Selenium");//11Selenium
System.out.println("Selenium" + " ");
System.out.println(" " + "Selenium");
String + String = Concatenation
String + Integer = Concatenation
Integer + Integer = Addition
------------------------------------
2) String Comparison
In figurer programming nosotros bring two types of comparison
i) 2-way Comparison (true/false)
ii) 3-way Comparison (0, > 0, < 0)
--------------------------------
a) String comparing using (==) Relational Operator
It supports 2-way Comparison(true/false)
--------------------------------
b) Sting comparing using equals() method
It supports 2-way Comparison(true/false)
-----------------------------------
c) Sting comparing using compareTo() method
It supports 3-way Comparison (0, >0, <0)
------------------------------------
// H5N1 to Z (65 to 90)
// a to z (97 to 122)
// 0 to nine (48 to 57)
Example:
String str1 = "selenium";
String str2 = "SELENIUM";
String str3 = "SELENIUM";
String str4 = "zselenium";
//String Comparison using == Operator
System.out.println(str1 == str2);//false
System.out.println(str2 == str3);//true
//String Comparison using equals() method
System.out.println(str1.equals(str2));//false
System.out.println(str2.equals(str3));//true
//String Comparison using compareTo() method
System.out.println(str1.compareTo(str2));//Greater than 0
System.out.println(str2.compareTo(str3));//0
System.out.println(str1.compareTo(str4));//Less than 0
--------------------------------
Result Criteria for 3-way comparison
if str1 = str2 hence 0
if str1 > str2 hence > 0
if str1 < str2 hence < 0
----------------------------------------------
ii) Arrays inwards Java
a) What is Java Array?
> Java Array is an Object that holds a fixed release of values of a unmarried information type.
> The length of Array is established when the Array is created.
> Array length is fixed, index starts from cipher to n-1.
b) Creating of Arrays
1st Method
dataType arrayName []; //Creating Array
arrayName = novel dataType[size]; //Define Size
arrayName[0]=value;//Assign value
arrayName[1]=value;
arrayName[2]=value;
.
.
----------------------------------
Example:
int a [];
a = novel int[3];
a[0]=10;
a[1]=20;
a[2]=30;
System.out.println(a[0]);//10
System.out.println(a[1] + a[2]);//50
-------------------------------------
//Assign values to elements that to a greater extent than than the length of Array (Run-Time Error)
int a [];
a = novel int[3];
a[0]=10;
a[1]=20;
a[2]=30;
a[3]=40;//Out of Range(Run-Time Error)
System.out.println(a[0]);//10
System.out.println(a[1] + a[2]);//50
------------------------------------
//Assign values to roughly elements alone (No Error)
int a [];
a = novel int[3];
a[1]=20;
a[2]=30;
System.out.println(a[1] + a[2]);//50
------------------------------------------------------
//If nosotros assign invalid values (data type) -Syntax Error
int a [];
a = novel int[3];
a[0] =1.23; //Syntax Error
a[1]=20;
a[2]=30;
System.out.println(a[1] + a[2]);//50
------------------------------------------------
2nd Method
dataType [] arrayName= novel dataType[length]; //Declare Array amongst length
arrayName[index] = value; //Assign value
Example:
int [] abc = novel int [4];
abc[0] =10;
System.out.println(abc[0]); //10
------------------------------
3rd Method (Declare Array as well as Assign values)
dataType [] arrayName = {value1, value2, value3}
Example:
int [] xyz = {10, 20, 30, 40};
System.out.println(xyz[2]);//30
------------------------------------------------
Declaring unlike types of Arrays
Example:
char [] abc = {'A', 'B', 'Z'}; //Array of Characters
int [] xyz = {10, 20, 30, 40}; //Array of Integers
String [] a = {"UFT", "Selenium", "RFT"}; //Array of Strings
boolean [] b ={true, false, false, true}; //Array of Boolean values
System.out.println(abc[1]);//B
System.out.println(xyz[3]);//40
System.out.println(a[1]);//Selenium
System.out.println(b[2]);//false
---------------------------------------------
c) Copy Values from i to another
int [] array1 = {1, 2, 3, 4, 5};
int array2 [] = array1;
System.out.println(array2[2]);//3
for (int i =0; i < array2.length; i++){
System.out.println(array2[i]);
}
------------------------------------------
d) Types of Arrays
Two types of Arrays
1) Single dimensional Array
2) Multi dimensional Array
Example:
int [] array1 = {1, 2, 3, 4, 5};//Single dimensional Array
int [] [] array2 = {{1, 3, 5, 7}, {2, 4, 6, 8}};// Multi dimensional Array
System.out.println(array2[0][0]);//1
System.out.println(array2[1][0]);//2
System.out.println(array2[1][2]);//6
--------------------------------------
Assignment
Print Multi dimensional Array (2D Array) values using Nested for loop.
----------------------------------
e) Advantages & Disadvantages of Arrays
Advantages:
Using Arrays nosotros tin cease optimize the code, information tin cease endure retrieved easily.
We tin cease choke required information using index position
Disadvantages:
We tin cease shop fixed release of Elements only.
It doesn't modify its size during execution.
---------------------------------------------------
i) String Handling inwards Java
ii) Arrays inwards Java
-------------------------------
i) String Handling inwards Java
a) What is String?
> String is a sequence of characters written double quotes.
------------
Numbers
-------------
Integers - byte, short, int, long information types
Floating betoken values/decimal values- float, double information types
-----------------------------
Character -char
------------------
Logical values
boolean
----------------------
String -Object
----------------
> String may bring Alphabets, Numbers as well as Special characters.
Example:
System.out.println("Selenium");//Selenium
System.out.println("123Selenium");//123Selenium
System.out.println("Selenium*&123");//Selenium*&123
System.out.println("1234");//1234
--------------------------------
b) Create Strings
String myTool ="Selenium";//String Variable
String [] myTools ={"UFT", "Selenium", "LoadRunner", "RFT"}; //Array of Strings
System.out.println(myTool);//Selenium
for (String tool: myTools){
System.out.println(tool);
}
------------------------------------
c) Operations on Strings
1) Concatenating Strings
String str1 = "Selenium ";
String str2 ="Testing";
System.out.println(str1 + str2);//Selenium Testing
System.out.println("Selenium" + (1 + 1));//Selenium2
System.out.println("Selenium" + 1 + 1);//Selenium11
System.out.println(1 + 1 + "Selenium");//2Selenium
System.out.println("1" + 1 + "Selenium");//11Selenium
System.out.println("Selenium" + " ");
System.out.println(" " + "Selenium");
String + String = Concatenation
String + Integer = Concatenation
Integer + Integer = Addition
------------------------------------
2) String Comparison
In figurer programming nosotros bring two types of comparison
i) 2-way Comparison (true/false)
ii) 3-way Comparison (0, > 0, < 0)
--------------------------------
a) String comparing using (==) Relational Operator
It supports 2-way Comparison(true/false)
--------------------------------
b) Sting comparing using equals() method
It supports 2-way Comparison(true/false)
-----------------------------------
c) Sting comparing using compareTo() method
It supports 3-way Comparison (0, >0, <0)
------------------------------------
// H5N1 to Z (65 to 90)
// a to z (97 to 122)
// 0 to nine (48 to 57)
Example:
String str1 = "selenium";
String str2 = "SELENIUM";
String str3 = "SELENIUM";
String str4 = "zselenium";
//String Comparison using == Operator
System.out.println(str1 == str2);//false
System.out.println(str2 == str3);//true
//String Comparison using equals() method
System.out.println(str1.equals(str2));//false
System.out.println(str2.equals(str3));//true
//String Comparison using compareTo() method
System.out.println(str1.compareTo(str2));//Greater than 0
System.out.println(str2.compareTo(str3));//0
System.out.println(str1.compareTo(str4));//Less than 0
--------------------------------
Result Criteria for 3-way comparison
if str1 = str2 hence 0
if str1 > str2 hence > 0
if str1 < str2 hence < 0
----------------------------------------------
ii) Arrays inwards Java
a) What is Java Array?
> Java Array is an Object that holds a fixed release of values of a unmarried information type.
> The length of Array is established when the Array is created.
> Array length is fixed, index starts from cipher to n-1.
b) Creating of Arrays
1st Method
dataType arrayName []; //Creating Array
arrayName = novel dataType[size]; //Define Size
arrayName[0]=value;//Assign value
arrayName[1]=value;
arrayName[2]=value;
.
.
----------------------------------
Example:
int a [];
a = novel int[3];
a[0]=10;
a[1]=20;
a[2]=30;
System.out.println(a[0]);//10
System.out.println(a[1] + a[2]);//50
-------------------------------------
//Assign values to elements that to a greater extent than than the length of Array (Run-Time Error)
int a [];
a = novel int[3];
a[0]=10;
a[1]=20;
a[2]=30;
a[3]=40;//Out of Range(Run-Time Error)
System.out.println(a[0]);//10
System.out.println(a[1] + a[2]);//50
------------------------------------
//Assign values to roughly elements alone (No Error)
int a [];
a = novel int[3];
a[1]=20;
a[2]=30;
System.out.println(a[1] + a[2]);//50
------------------------------------------------------
//If nosotros assign invalid values (data type) -Syntax Error
int a [];
a = novel int[3];
a[0] =1.23; //Syntax Error
a[1]=20;
a[2]=30;
System.out.println(a[1] + a[2]);//50
------------------------------------------------
2nd Method
dataType [] arrayName= novel dataType[length]; //Declare Array amongst length
arrayName[index] = value; //Assign value
Example:
int [] abc = novel int [4];
abc[0] =10;
System.out.println(abc[0]); //10
------------------------------
3rd Method (Declare Array as well as Assign values)
dataType [] arrayName = {value1, value2, value3}
Example:
int [] xyz = {10, 20, 30, 40};
System.out.println(xyz[2]);//30
------------------------------------------------
Declaring unlike types of Arrays
Example:
char [] abc = {'A', 'B', 'Z'}; //Array of Characters
int [] xyz = {10, 20, 30, 40}; //Array of Integers
String [] a = {"UFT", "Selenium", "RFT"}; //Array of Strings
boolean [] b ={true, false, false, true}; //Array of Boolean values
System.out.println(abc[1]);//B
System.out.println(xyz[3]);//40
System.out.println(a[1]);//Selenium
System.out.println(b[2]);//false
---------------------------------------------
c) Copy Values from i to another
int [] array1 = {1, 2, 3, 4, 5};
int array2 [] = array1;
System.out.println(array2[2]);//3
for (int i =0; i < array2.length; i++){
System.out.println(array2[i]);
}
------------------------------------------
d) Types of Arrays
Two types of Arrays
1) Single dimensional Array
2) Multi dimensional Array
Example:
int [] array1 = {1, 2, 3, 4, 5};//Single dimensional Array
int [] [] array2 = {{1, 3, 5, 7}, {2, 4, 6, 8}};// Multi dimensional Array
System.out.println(array2[0][0]);//1
System.out.println(array2[1][0]);//2
System.out.println(array2[1][2]);//6
--------------------------------------
Assignment
Print Multi dimensional Array (2D Array) values using Nested for loop.
----------------------------------
e) Advantages & Disadvantages of Arrays
Advantages:
Using Arrays nosotros tin cease optimize the code, information tin cease endure retrieved easily.
We tin cease choke required information using index position
Disadvantages:
We tin cease shop fixed release of Elements only.
It doesn't modify its size during execution.
---------------------------------------------------