12 – Strings and Arrays in Java

Selenium Class 12 – Strings and Arrays in Java

i) String handling in Java
ii) Arrays in Java

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″;
int c=100;
String d=”India123″;
string e=”$%^”;
String f= “India123#$”;

Operations on Strings:

1) Concatenating Strings

String + String – String
String + Number – String
Number + Number – Addition

Example:

String str1= “Selenium”;
String str2 = ” Testing”;
//String concatenation using + operator
System.out.println(str1+str2);//Selenium Testing

//String concatenation using “concat” Method
System.out.println(str1.concat(str2));//Selenium Testing

System.out.println(“Selenium”+”Testing”);//SeleniumTesting
System.out.println(“Selenium”+1+1);//Selenium11
System.out.println(1+1+”Selenium”);//2Selenmium
System.out.println(1+11);//12

2) String Comparison

In Computer programming we have two types of comparisons,
1) 2-way Comparison (Logical/ true or false)
2) 3-way Comparison (Zero, Greater than Zero, Less than Zero)

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 > string 2 then Positive Value
if string1 < string 2 then Negative value

Comparing two number – based on their values (3>2)
Comparing two strings – based on ANSI values

ANSI character codes

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

Example:

String str1 = “SELENIUM”;
String str2 = “selenium”;
String str3 = “SELENIUM”;
String str4 = “zseleniu”;

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

//String Comparison using equals() Method
System.out.println(str1.equals(str2));//false
System.out.println(str1.equals(str3));//true

//String comparison using compareTo()
System.out.println(str1.compareTo(str2));//Negative value
System.out.println(str1.compareTo(str3));//0
System.out.println(str4.compareTo(str1));//Positive value

ii) Arrays in Java

  • In Java, Array is an Object that holds a fixed number of values of a single data type
  • The length of Array is established when the Array is created.
  • Array length is fixed and index starts from zero to n-1,

Declaration of Arrays

1st Method

Syntax:
dataType arrayName[]; Declare an Array/ Create an Array
arrayName = new dataType[size]; //Define size
arrayName[index] = value; //Assign a 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 Array 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 Array Elements only

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

System.out.println(a[0] + a[1]);//30

Assign different type of data to Array Elements (Syntax Error)

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

System.out.println(a[0] + a[1]);//30

2nd Method

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

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

3rd Method

dataType [] arrayName = {value1, value2, value3, value4};//Create an Array with Initialization

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

Creating different type Arrays

int [] a= {10, 20, 30, 40, 50}; //Array of Integers

char [] b= {‘A’, ‘S’, ‘1’, ‘*’};//Array of Characters

String [] c = {“UFT”, “Selenium”, “RFT”, “SilkTest”};//Array of Strings

double [] d ={1.234, 3.456, 6.45, 7.890}; // Array of decimal point values

boolean [] e = {true, true, false, true, false}; //Array of Boolean Values / Logical Values

System.out.println(a[2]);//30
System.out.println(d[3]);//7.890
System.out.println(e[0]);//true