17 – Java Predefined Methods

Selenium Class 17 – Java Predefined Methods

Categories of Predefined Methods

i) String Methods
ii) Number Methods
iii) Character Methods
iv) Array Methods
Etc…

i) String Methods

1) compareTo() Method

It Compares two strings and supports 3-way comaprison

a) Result criteria for 2-way Comparison
if string1 == string2 then true
if string1 != string2 then false

b) Result criteria for 3-way Comparison
if string1 == string2 then 0
if string1 > string2 then Positive Value
if string1 < string2 then Negative Value

Comparing two numbers – based on their values
Comparing two strings – based on ANSI charcter codes

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

5 > 3 – true
“UFT” > “Selenium” – true

Example:

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

System.out.println(str1.compareTo(str2));//Positive Values
System.out.println(str1.compareTo(str3));//Negative Value
System.out.println(str1.compareTo(str4));//0

2) equals() Method

It Compares two strings and supports 2-way comaprison

Example:
String str1= “selenium”;
String str2= “SELENIUM”;
String str3 = “selenium”;
System.out.println(str1.equals(str2));//false
System.out.println(str1.equals(str3));//true

3) Cocat() Method

It concat two strings
String str1= “Selenium”;
String str2= ” Testing”;

System.out.println(str1.concat(str2));//Selenium Testing
System.out.println(“Selenium”.concat(” Java”));//Selenium Java

4) charAt() Method

Returns a Character value by Index

Example:
String str1= “Selenium”;

System.out.println(str1.charAt(1));//e
System.out.println(str1.charAt(7));//m

5) equalsIngoneCase();

It compares two strings and ignores letters (Upper case or Lower case)

Example:
String str1 = “selenium”;
String str2 = “SELENIUM”;
String str3 = “UFT”;

System.out.println(str1.equalsIgnoreCase(str2));//true
System.out.println(str2.equalsIgnoreCase(str3));//false

6) toUpperCase() Method

It conversts values to Upper Case

Example:
String str1 = “selenium”;
String str2 = “SELENIUM”;
String str3 = “SELEnium”;
String str4 = “Selenium123”;

System.out.println(str1.toUpperCase()); //SELENIUM
System.out.println(str2.toUpperCase());//SELENIUM
System.out.println(str3.toUpperCase());//SELENIUM
System.out.println(str4.toUpperCase());//SELENIUM123

7) toLowerCase() Method

It converts values to lower case

Example:
String str1 = “selenium”;
String str2 = “SELENIUM”;
String str3 = “SELEnium”;
String str4 = “Selenium123”;

System.out.println(str1.toLowerCase()); //selenium
System.out.println(str2.toLowerCase());//selenium
System.out.println(str3.toLowerCase());//selenium
System.out.println(str4.toLowerCase());//selenium123

8) trim() Method

It removes spaces from both sides of a string

Example:
String str = ” Selenium “;
System.out.println(str);
System.out.println(str.trim());

9) substring() method

Returns part of a string based on index position/s

Example:
String str = “Welcome to Selenium Testing”;

System.out.println(str.substring(11));//Selenium Testing
System.out.println(str.substring(20));//Testing
System.out.println(str.substring(11, 19));//Selenium
System.out.println(str.substring(8, 10));//to

10) endsWith() Method

It checks if a string ends with specified suffix or not? And supports 2-way comparison (true/false)

Example:
String str = “Welcome to Selenium Testing”;

System.out.println(str.endsWith(“Selenium Testing”));//true
System.out.println(str.endsWith(“Testing”));//true
System.out.println(str.endsWith(“Selenium”));//false

11) length property

Returns length of a String

String str = “Selenium Testing”;
String str2 = “Selenium”;
System.out.println(str.length());//16
System.out.println(str2.length());//8

core java tutorial
Java Tutorial for Selenium

ii) Number Methods

1) compareTo() method

Integer Class wraps a value of primitive data type int ia an Object

Comparing two numbers in 3-way comparison
Number1 == Number2 then 0
Number1 > Number2 then 1
Number1 < Number2 then -1

Example:

int x=7;
Integer a=x;
//Making primitive Data Type x is an Integer Object using Integer Class…

System.out.println(a.compareTo(7));//0 System.out.println(a.compareTo(5));//1
System.out.println(a.compareTo(9));//-1

2) equals() Method

It compares two numbers and supports 2-way comparison
int x=7;
Integer a=x;
System.out.println(a.equals(7));//true
System.out.println(a.equals(6));//false
System.out.println(a.equals(9));//false

3) abs() Method

It Returns absolute value

double a = 10.234;
double b = 10.789;
double c= -20.345;
int d=100;
int e=-23;

System.out.println(Math.abs(a));//10.234
System.out.println(Math.abs(b));//10.789
System.out.println(Math.abs(c));//20.345
System.out.println(Math.abs(d));//100
System.out.println(Math.abs(e));//23

4) round () Method

It rounds a value to nearest Integer

example:
double a=10.456;
double b=10.897;
double c=-10.345;

System.out.println(Math.round(a));//10
System.out.println(Math.round(b));//11
System.out.println(Math.round(c));//-10

5) min() Method

It returns minimum value between two numbers

Example:
int a=5;
int b=7;
double c=10.234;
double d = 10.789;

System.out.println(Math.min(a, b));//5
System.out.println(Math.min(c, d));//10.234
System.out.println(Math.min(11, 17));//11
System.out.println(Math.min(109.34, 103.45));//103.45

6) max() method

It returns maximum value between two numbers
int a=5;
int b=7;
double c=10.234;
double d = 10.789;
System.out.println(Math.max(a, b));//7
System.out.println(Math.max(c, d));//10.789
System.out.println(Math.max(9, 7));//9
System.out.println(Math.max(109.34, 103.45));//109.34

7) Random Number

It generates a Random Number

Example:
System.out.println(Math.random());

Selenium 2019 Training Videos