Selenium Tutorial For Beginners 12|Java Strings, and Arrays|G C Reddy|

preview_player
Показать описание
Java Programming for Selenium, Java Language Fundamentals, String Handling in Java, Create Strings, String Concatenation, and String comparison in Java.
Two-way comparison, and 3-way comparison in Computer programming, and Types of Output in Computer programming. Arrays in Java, Declare different types of Arrays in Java, Display Array element value, and display or print all elements of an Array.
Рекомендации по теме
Комментарии
Автор

Selenium Class 12 Notes:
Selenium Class 12: Java Strings, and Arrays

i) String Handling in Java
ii) Java Arrays

i) String Handling in Java

> What is String?

String is a sequence of characters written in double quotes

Syntax:

String stringName = "value";

String may have Alphabets, Numbers, and Special Characters

String a="India";
String b= "100";
String c ="India123";
String d ="*&%$";
String e = "123*&%$";
String f ="India123*&%$";
String g="Selenium With Java"

In VBScript:
Dim a
a=100 'Internally VBScript considers this data as Integer type data
.
.
a=12.345 'Internally VBScript considers this data as Double type data
.
.
.
a=#10/10/201# 'Internally VBScript considers this data as Date type data
.
.
.
a="Vijaya" 'Internally VBScript considers this data as String type data

Operations on Strings

1) Concatenating Strings

String + String - Concatenation
String + Integer - Concatenation
Integer + Integer - Addition

String str1 = "Selenium";
String str2="Testing";

System.out.println(str1 + str2);//SeleniumTesting


System.out.println("Selenium" + "Testing");//SeleniumTesting




2) String Comparison

In Computer programming we have two types of comparison,

1) 2-way Comparison (true/false)
2) 3-way Comparison (0, Greater than 0, Less than 0)

Ways of String Comparison

a) String Comparison using Relational operator (==)
It supports 2-way Comparison

b) String Comparison using equals() method
It supports 2-way Comparison

c) String Comparison using compareTo() method
It supports 3-way Comparison

Result Criteria for 3 way comparison
if string1 = string2 then 0
if string1 > string2 then Positive value
if string1 < string2 then Negative value

Comparing 2 Numbers - based on their values (3 > 2)
Comparing 2 Strings - based on ANSI values

ANSI character codes

A to Z (65 to 90)
a to z (97 to 122)
0-9 (48 to 57)

Example:
String str1 ="SELENIUM";
String str2 = "selenium";
String str3="SELENIUM";
String str4="zabc";

//String Comparison using Relational (==) Operator
System.out.println(str1 == str2);//false
System.out.println(str1 == str3);//true

//String Comparison using equals() method



//String Comparison using compareTo() method
value

Value
}
}

3) Find String Length

String str ="SELENIUM";


String str ="SELENIUM";
int size = str.length();
System.out.println(size);

ii) Java Arrays

> In Java, Array is Object that holds a fixed number of values of a single type
> The length of Array is established when the Array is created.
> Array length is fixed, index starts from zero.

Declaration of Arrays

1st Method

Syntax:

dataType arrayName[]; //Declare Array/Create Array
arrayName = new dataType[size]; //Define Size
arrayname[index] = value; //Assign value
.
.
.

Example:

int a[];
a= new int[3];
a[0]=10;
a[1]=20;
a[2]=30;
System.out.println(a[1] + a[2]);//50

Assign values to elements that more than the length of Array (Run-time Error)
int a[];
a= new int[3];
a[0]=10;
a[1]=20;
a[2]=30;
a[10]=40;
System.out.println(a[1] + a[2]);//50

Assign Values to some Elements only

int a[];
a= new int[3];
a[1]=20;
System.out.println(a[1]);//20

int a[];
a= new int[3];
a[0]=10;
a[1]=20;
a[2]=1.23;
System.out.println(a[1]);//20

2nd Method

dataType[] arrayName = new dataType[size]; //Create Array with length
arrayName[index] = value;//Assign Value
.
.

int [] a = new int[3];
a[0]=10;
a[1]=20;
a[2]=30;


3rd Method
dataType[] arrayName = {value1, value2, value3, value4};//Create Array and Assign
values

exmaple:
int a[] ={10, 20, 30, 40, 50};
System.out.println(a[1] + a[3]);//60

In VBScript

Dim a[4]
a[0] = 10 'Integer
a[1] = "India" 'String
a[2] =12.34 'Double
a[3] = #10/10/2010#

In Java

int [] a = {1, 2, 3, 4};

Creating different Types of Arrays

int [] a = {10, 20, 30, 40, 50};//Array of Integers
char [] b = {'A', 'B', 'z', '1', '*'}; //Array of Characters
String [] c = {"UFT", "Selenium", "RFT", "123"};//Array of Strings
double [] d = {1.234, 2.345, 3.456, 67.89};//Array of Decimal point values
boolean [] e = {true, false, false, true};//Array of Boolean / Logical Values
System.out.println(a[2]);//30


Copy values form one Array to another (Copy Array)

int [] array1 = {1, 2, 3, 4, 5};
int [] array2 = array1;





int [] array1 = {1, 2, 3, 4, 5};
int [] array2 = array1;




*/
//Print all elements an Array using for loop
for (int i=0; i <array2.length; i++){

}

//print all elements an Array using Enhanced for loop
for (int num: array2){
System.out.println(num);

//Copy Particular Element

int [] array1={1, 2, 3, 4, 5};
int [] array2 = {array1[2]};


int [] array2 = new int [4];
array2[2] = array1[2];

default value

Assignment to Vijaya: Print Two dimensional Array using nested for loop

int [] [] array = {{10, 20, 30, 40}, {15, 25, 35, 45}};

Advantages and Disadvantages of Arrays

Advantages:
> Using Arrays we can optimize code, data can be retrived easily.
> We can get required data using index position

Disadvantages:
> We can store fixed number of elements only, It doesn't change its size during
execution.

Java Array is Static Data Structure, and Java ArrayList is Dynamic Data Structure,

In VBScript:

Dim a, b(4), c(), d (4, 5)

a- Scalar Variable
b- Array Variable
c - Dynamic Array
d- Two dimensional Array

VBScript Java Perl

Scalar Variables Scalar Variables/Variables Scalar Variables/Variables
Array Variables Arrays Array Variables
Dictionary Object Array List Associated Arrays / Hash Variables

gcreddy