Java for Selenium Part 9


Java Built-in Methods

Categories of Built in 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 comparison

Result Criteria for 3-way comparison (Strings)

if string1 = string2 then 0
if string1 > string2 then >0/Positive value
if string1 < string2 then <0 / Negative Value

Comparing Numbers vs. Comparing Strings

Comparing Numbers based on Values

int a =10, b=20;
if (a>b){
Statements
---------
------
}

Comparing Strings based on ANSI Character codes

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

Example:

public static void main(String[] args) {
String str1 = "selenium";
String str2 = "SELENIUM";
String str3 = "seleniuma";
String str4 ="selenium";

System.out.println(str1.compareTo(str2));//>0 / Positive value
System.out.println(str1.compareTo(str3));//<0 / Negative Value
System.out.println(str1.compareTo(str4));//0
}
--------------------------------
2) equals() Method

Compares two strings and supports 2-way comparison

Result Criteria for 2-way comparison

if string1 = string2 then true
if string1 != string2 then false

Example:

public static void main(String[] args) {
String str1 = "selenium";
String str2 = "SELENIUM";
String str3 = "seleniuma";
String str4 ="selenium";

System.out.println(str1.equals(str2));//false
System.out.println(str1.equals(str4));//true
System.out.println(str1.equals("SELENIUM"));//false

System.out.println(str1 == str2);//false
System.out.println(str1 == str4);//true
}
--------------------------------
3) concat()

It concatenates two strings

public static void main(String[] args) {
String str1 = "Selenium ";
String str2 = "Testing";

System.out.println(str1.concat(str2));//Selenium Testing
System.out.println(str1 + str2);//Selenium Testing
}
--------------------------------
4) charAt() method

Returns a Character value by index

Example:

public static void main(String[] args) {
String str1 = "Selenium ";

System.out.println(str1.charAt(4));//n
System.out.println(str1.charAt(40));//Run-time Error
--------------------------------
5) equalsIgnoreCase()

It compares two strings and supports 2-way comparison

It ignores cases (Upper Case or Lower Case)

Example:

public static void main(String[] args) {
String str1 = "selenium";
String str2 = "SELENIUM";
String str3 = "UFT";

System.out.println(str1.equalsIgnoreCase(str2));//true
System.out.println(str1.equalsIgnoreCase(str3));//false
}
--------------------------------
6) toUpperCase()

It converts values to Upper case

Example:

public static void main(String[] args) {
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
System.out.println(str5.toUpperCase());//S*&^
}
--------------------------------
7) toLowerCase()

Converts values to Lower case and it ignores Numbers and special characters

Example:

public static void main(String[] args) {
String str1 = "selenium";
String str2 = "SELENIUM";
String str3 = "SELEnium";
String str4 = "selenium123";
String str5 ="S*&^";

System.out.println(str1.toLowerCase());//selenium
System.out.println(str2.toLowerCase());//selenium
System.out.println(str3.toLowerCase());//selenium
System.out.println(str4.toLowerCase());//selenium123
System.out.println(str5.toLowerCase());//s*&^
--------------------------------
8) trim()

Removes spaces from both the sides of a String

Example:

public static void main(String[] args) {
String str1 = "          Selenium              ";

System.out.println(str1);
System.out.println(str1.trim());
}
--------------------------------
9) substring()

It returns sub string based on index

Example:

public static void main(String[] args) {
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
}
--------------------------------
10) endsWith()

Ends with specified suffix, and provides Logical (true/false) Result.

Example:

public static void main(String[] args) {
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("ing"));//true
System.out.println(str.endsWith("Selenium"));//false
}
--------------------------------
11) length

Returns length of a String

Example:

public static void main(String[] args) {
String str1 = "Selenium Testing";
String str2 = "Selenium";
System.out.println(str1.length());//16
System.out.println(str2.length());//8
}
-----------------------------------------------------
ii) Number Methods

//Integer Class wraps a value of primitive datatype int in an Object
//An Object of Integer contains a single field whose type is int
--------------------------------
1) compareTo()

Compares two integers and supports 3-way comparison

Example:

Result Criteria for 3 way Comparison (Integers/Numbers)

if Integer1 = Integer2 then 0
if Integer1 > Integer2 then 1
if Integer1 < Integer2 then -1

public static void main(String[] args) {
int a = 5;
Integer b =a;

System.out.println(b.compareTo(7));//-1
System.out.println(b.compareTo(5));//0
System.out.println(b.compareTo(4));//1
--------------------------------
2) equals()

Supports 2 way comparison

Example:

public static void main(String[] args) {
int a = 5;
Integer b =a;

System.out.println(b.equals(7));//false
System.out.println(b.equals(5));//true
}
--------------------------------
3) abs()

Returns absolute value

Example:

public static void main(String[] args) {
double a = 10.456;
double b = 10.786;
double d = -12.34;
int c=7;
int e = -100;

System.out.println(Math.abs(a));//10.456
System.out.println(Math.abs(b));//10.786
System.out.println(Math.abs(c));//7
System.out.println(Math.abs(d));//12.34
System.out.println(Math.abs(e));//100
}
--------------------------------
4) round()

Rounds the value to nearest integer

Example:
public static void main(String[] args) {
double a = 10.456;
double b = 10.786;
double d = -12.34;
int c=7;
int e = -100;

System.out.println(Math.round(a));//10
System.out.println(Math.round(b));//11
System.out.println(Math.round(c));//7
System.out.println(Math.round(d));//-12
System.out.println(Math.round(e));//-100
}
--------------------------------
5) min()

Returns minimum value between two numbers

example:

public static void main(String[] args) {
double a = 10.456;
double b = 10.786;
int c =10;
int d= 8;

System.out.println(Math.min(a, b));//10.456
System.out.println(Math.min(c, d));//8
System.out.println(Math.min(5, 4));//4
System.out.println(Math.min(1.234, 2.123));//1.234
System.out.println(Math.min(-10, -8));//-10
System.out.println(Math.min(-1.234, -2.123));//-2.123
}
}
--------------------------------
6) max()

Returns maximum value between two numbers

Example:

public static void main(String[] args) {
double a = 10.456;
double b = 10.786;
int c =10;
int d= 8;

System.out.println(Math.max(a, b));//10.786
System.out.println(Math.max(c, d));//10
System.out.println(Math.max(5, 4));//5
System.out.println(Math.max(1.234, 2.123));//2.123
System.out.println(Math.max(-10, -8));//-8
System.out.println(Math.max(-1.234, -2.123));//-1.234
}
--------------------------------
7) random()

Generates a Random Number

Example:

public static void main(String[] args) {
System.out.println(Math.random());//
}
-----------------------------------------------------
iii) Character Methods

// The Character class wraps a value of primitive data type char in an object

1) isLetter()

Checks if the value is Alpha byte or not? and returns Logical (true/false) result.

public static void main(String[] args) {
char a ='A';
char b ='a';
char c ='1';
char d = '*';

System.out.println(Character.isLetter(a));//true
System.out.println(Character.isLetter(b));//true
System.out.println(Character.isLetter(c));//false
System.out.println(Character.isLetter(d));//false
System.out.println(Character.isLetter('Z'));//true
System.out.println(Character.isLetter('7'));//false
}
--------------------------------
2) isAlphabetic()

Checks if the value is Alpha byte or not? and returns Logical (true/false) result.

Example:

public static void main(String[] args) {
char a ='A';
char b ='a';
char c ='1';
char d = '*';

System.out.println(Character.isAlphabetic(a));//true
System.out.println(Character.isAlphabetic(b));//true
System.out.println(Character.isAlphabetic(c));//false
System.out.println(Character.isAlphabetic(d));//false
System.out.println(Character.isAlphabetic('Z'));//true
System.out.println(Character.isAlphabetic('7'));//false
}
--------------------------------
Assignment:

What is the difference between isLetter and isAlphabetic methods.
--------------------------------
3) isDigit

Checks if the value is Number or not?

public static void main(String[] args) {
char a ='A';
char b ='1';

System.out.println(Character.isDigit(a));//false
System.out.println(Character.isDigit(b));//true
System.out.println(Character.isDigit('B'));//false
System.out.println(Character.isDigit('9'));//true
}
--------------------------------
4) isUpperCase()

Checks if the value is Upper Case or not?

5) isLowerCase

Checks if the value is Lower Case or not?

Examples:

public static void main(String[] args) {
char a ='A';
char b ='a';
char c ='1';

System.out.println(Character.isUpperCase(a));//true
System.out.println(Character.isUpperCase(b));//false
System.out.println(Character.isUpperCase(c));//false

System.out.println(Character.isLowerCase(a));//false
System.out.println(Character.isLowerCase(b));//true
}
-----------------------------------------------------
iv) Array Methods

1) length()

It returns length of an Array

Example:

public static void main(String[] args) {
int [] array1 = {10, 20, 30, 40, 5};
System.out.println(array1.length);//5
}
--------------------------------
2) toString()

Prints an Array

public class Class1 {

public static void main(String[] args) {
String [] array1 = {"Selenium", "UFT", "RFT", "SilkTest"};

String str = Arrays.toString(array1);
System.out.println(str); //[Selenium, UFT, RFT, SilkTest]

for (int i=0; i < array1.length; i++){
System.out.println(array1[i]);
}
--------------------------------
3) contains()

Checks if the Array contains certain value or not?, retruns boolean result.

Example:


public static void main(String[] args) {
String [] array1 = {"Selenium", "UFT", "RFT", "SilkTest"};
boolean a = Arrays.asList(array1).contains("UFT");//
System.out.println(a);//true

System.out.println(Arrays.asList(array1).contains("Selenium"));//true
System.out.println(Arrays.asList(array1).contains("Java"));//false
}
--------------------------------
Method Syntax:

object.method();
class.method();
Class/object.property.method();
etc...
-----------------------------------------------------

0 comments:

Post a Comment