18 – Java Predefined Methods Part-2
Selenium Class 18 – Java Predefined Methods Part-2
Categories of Predefined Methods
i) String Methods
ii) Number Methods
iii) Character Methods
iv) Array Methods
etc…
iii) Character Methods
1) isLetter () Method
It checks if a value is Alphabet or not? And returns boolean result
Example:
char a=’Z’;
char b=’1′;
System.out.println(Character.isLetter(a));//true
System.out.println(Character.isLetter(b));//false
System.out.println(Character.isLetter(‘B’));//true
System.out.println(Character.isLetter(‘2’));//false
System.out.println(Character.isLetter(‘*’));//false
2) isDigit()
It checks if a Value is number or not?
Example:
char a=’Z’;
char b=’1′;
System.out.println(Character.isDigit(a));//false
System.out.println(Character.isDigit(b));//true
System.out.println(Character.isDigit(‘4’));//true
System.out.println(Character.isDigit(‘C’));//false
System.out.println(Character.isDigit(‘%’));//false
3) isLowerCase()
It checks if a value is Lower case or not? and returns boolean result
Example:
char a=’Z’;
char b=’a’;
char c=’1′;
System.out.println(Character.isLowerCase(a));//false
System.out.println(Character.isLowerCase(b));//true
System.out.println(Character.isLowerCase(a));//false
System.out.println(Character.isLowerCase(‘s’));//true
System.out.println(Character.isLowerCase(‘Z’));//false
System.out.println(Character.isLowerCase(‘4’));//false
4) isUpperCase
It checks if a value is upper case value or not?
char a=’Z’;
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.isUpperCase(‘s’));//false
System.out.println(Character.isUpperCase(‘Z’));//true
System.out.println(Character.isUpperCase(‘4’));//false
iv) Array Methods
1) length()
It returns length of an Array
Example:
int [] a= {10, 20, 30, 40, 50};
System.out.println(a.length);//5
2) Copy an Array to a String
int [] a= {10, 20, 30, 40, 50};
String str= Arrays.toString(a);
System.out.println(str);
String [] a = {“UFT”, “Selenium”, “RFT”, “SilkTest”};
String str = Arrays.toString(a);
System.out.println(str);
3) Print Array using for loop
public static void main(String[] args) {
String [] a = {“UFT”, “Selenium”, “RFT”, “SilkTest”, “LoadRunner”};
for (int i=0; i<=a.length-1; i++){
System.out.println(a[i]);
4) Print Array using enhanced for loop
String [] a = {“UFT”, “Selenium”, “RFT”, “SilkTest”, “LoadRunner”};
for (String val: a){
System.out.println(val);
}
5) Check if an Array contains certain value or not?
String [] a = {“UFT”, “Selenium”, “RFT”, “SilkTest”, “LoadRunner”};
boolean val = Arrays.asList(a).contains(“UFT”);
boolean val2 = Arrays.asList(a).contains(“Java”);
System.out.println(val);//true
System.out.println(val2);//false
Java ArrayList
Java Array is Static Data Structure and ArrayList is Dynamic Data Structure
Array versus ArrayList in Java
Array is Static (its size is fixed), ArrayList is Dynamic….
Java Array Example:
int [] a= new a[4];
a[0]=10;
a[1]=20;
a[2]=30;
a[3]=40;
a[4]=50; //Error
- ArrayList is Dynamic and you can add or remove elements….
Note: ArrayList is predefined class that we have to import from java.util package
Example:
1) Create Integer type ArrayList and conduct operations
ArrayList abc = new ArrayList<>();
//Add Elements to ArrayList
abc.add(100);
abc.add(200);
abc.add(300);
//print an ArrayList Element
int a= abc.get(1);
System.out.println(a);//200
//Or
System.out.println(abc.get(1));//200
//Return Size of ArrayList
int size = abc.size();
System.out.println(size);//3
//Remove an Element from Arraylist
abc.remove(1);
System.out.println(abc.size());//2
System.out.println(abc.get(1));//
//Check the Existence of Elements
System.out.println(abc.contains(100));//true
System.out.println(abc.contains(200));//false
abc.clear();
System.out.println(abc.size());//0
abc.add(200);
abc.add(100);
System.out.println(abc.get(1));
2) Create String Type Array
ArrayList abc = new ArrayList<>();
abc.add(“Selenium”);
abc.add(“UFT”);
abc.add(“RFT”);
String a= abc.get(2);
System.out.println(a);//RFT
3) Create Character type ArrayList
ArrayList xyz = new ArrayList<>();
xyz.add(‘A‘);
System.out.println(xyz.get(0));//A
Java Data Type Conversion
To conduct Mathematical Operations sometimes we need to convert the data
1) Integer type to byte type
int a=10;
byte b=20;
System.out.println(a+b);//30
int c=a+b;
System.out.println(c);//30
byte d= (byte)(a+b);
System.out.println(d);//30
Note: If you want convert byte to integer then no conversion is required, want to convert integer to byte then data conversion is required
byte a=10;
int b=a;
int c=20;
byte d=(byte)c;
System.out.println(d);
String to Integer
Note: Whenever we read data (from files or application objects) then computer program considers the data as String type data, in order to conduct mathematical operations then we need to convert the data
*We can’t convert alphabets to numbers
1) String to Number (Integer)
String a=”10″;
String b=”20″;
System.out.println(a+b);//1020
int num1 = Integer.parseInt(a);
int num2= Integer.parseInt(b);
System.out.println(num1+num2);//30
System.out.println(Integer.parseInt(a) + Integer.parseInt(b));//30
String c=”abcd”;
System.out.println(Integer.parseInt(c));//Run-time error
2) String to Number (Number with decimal places)
String a=”10.2345″;
double val = Double.parseDouble(a);//10.2345
//int val3 = Integer.parseInt(a);