Java Basics
Java Basics
CONTROL STATEMENTS
1) Conditional Statements
2) Looping/Iterative Statements -
3) Jumping Statements
if(weekno==1) {System.out.println("Thursday");}
{System.out.println("Tuesday");} else
{System.out.println("Wednesday");}
Looping statements : loop or iteration - executing same set of statements multiple times.
}while(i>=1);
Jumping Statements
for (int i=1;i<=10;i++) for(int i=1;i<=10;i++) for(int i=1;i<=10;i++)
{ { {
if(i==5) if(i==5) if(i==5 || i==7 ||i==9)
{ { {
break; continue; continue;
} } }
System.out.println(i); System.out.println(i); System.out.println(i);
} } }
it will break and not it will skip the number 5 it will skip the number 5 7
proceed further after the and continue 9 and continue
condition //1 2 3 4 //1 2 3 4 6 7 8 9 10 // 1 2 3 4 6 8 10
Arrays
Arrays - Array is collection of elements of same data type (Homogeneous data).
We can store multiple values into a single variable.
1) Single dimensional 2) Two Dimensional/Double dimensional
int a[]=new int[100]; here 100 is array size/ int a[][]=new int[3][2];
number of values we can store. int [][]a=new int[3][2];
int []a=new int[100]; int []a[]=new int[3][2];
5) read multiple values from an array 4) read single value from an array
System.out.println(Arrays.toString(a)); System.out.println(a[1][1]); //400
//[10, 20, 30, 40, 50]
5) reading data from 2D array
//normal for loop for(int i=0;i<=a.length-1;i++)
for(int i=0;i<=a.length-1;i++) {
{ for(int j=0;j<=a[i].length-1;j++ )
System.out.println(a[i]); {
} System.out.print(a[i][j]);
//[10, 20, 30, 40, 50] }
}
//enhanced for loop / for..each loop //Enhanced for loop
for(int value:a) for(int arr[]:a)
{ {
System.out.println(value); for(int x:arr)
} {
//[10, 20, 30, 40, 50] System.out.print(x);
}
}
//100 200 300 400 500 600
How to take input from keyboard/ at run time? Scanner
1) Seaching an element in array
2) Find how many times a number is repeated..
3) Find number of even's and odd's in array
4) Sort elements in array
5) Reverse an array
Scanner sc=new Scanner(System.in);
//Integer
System.out.println("Enter a number:");
int a= sc.nextInt();
System.out.println("Given value of a is:"+ a);
//Decimal
System.out.println("Enter a decimal number:");
double num=sc.nextDouble();
System.out.println("given decimal number is:"+num);
//String
System.out.println("Enter your name:");
String name=sc.next();
System.out.println("Your name is:"+name);
System.out.println(s1+s2); System.out.println(s.replace('e','X'));
System.out.println(s1.concat(s2)); //wXlcomX to sXlXnium java python
//welcome to java
System.out.println(s1+s2+s3); System.out.println(s.replace("python","C
System.out.println(s1.concat(s2).concat(s3)); #"));
//welcome to java programming //welcome to selenium java C#
System.out.println("welcome"+"to java");
System.out.println("welcome".concat("to java"));
trim() - remove spaces right and left side charAt() - returns a character based on
String s=" welcome "; index
System.out.println("Before trimming:"+ s.length()); index starts from 0
//13 String s="welcome";
s1=s.trim(); System.out.println(s.charAt(3)); //c
System.out.println("After trimming:"+s1.length()); //7 System.out.println(s.charAt(5)); //m
Ex4
String s="abc,123@xyz";
String a1[]=s.split(","); //abc 123@xyz
System.out.println(a1[0]); //abc
System.out.println(a1[1]); //123@xyz