Uploaded by Code With Arun

Notes

advertisement
Looping Statements:
-------------------
while
do...while
for:
----
for(initialization, condition, increment/decrement) { //block of statements } Methods:
--------
-->block of code when called performs specific actions mentioned in it.
Syntax:
-------
modifier return_type nameOfMethod(Parameter List){
//method body
}
Two Ways call method with parameter
-----------------------------------
1.Passing Primitive Data Type Aruguments
Ex:
---
void m1(int a){.....}
void m2(int a,char b){...}
static void m3(String str){...}
2.Passing Reference Type Aruguments
------------------------------------
Ex:
---
void m1(Emp e){...}
void m2(Student s,School sc){...}
static void m3(Admin a,Employee e){....}
Methods return void:
--------------------
--> "void" it means doesn't return a value
Ex:
public void printMessage(String str){
S.O.Pln(Message);
}
Methods returns value:
----------------------
Ex:
---
public int square(int number){
return number*number;
}
int result=square(5);
S.O.Pln(result)
Method Overloading:
-------------------
class demo {
void m1(int a){....}
void m1(int b,String s){...}
void m1(char ch,double d){...}
}
Ex:
---
public class Calculator {
public int add(int a,int b){
return a+b;
}
public int add(double a,double b){
return a+b;
}
public int add(int a,int b,int c){
return a+b+c;
}
public static void main(String args[]){
Calculator Calc = new Calculator();
int result1 = Calc.add(10,20,30);
int result2 = Calc.add(100,250);
double result3 = Calc.add(10.5,5.6);
}
}
In-Out Implementation:
-----------------------
1.Scanner Class
----------------
Scanner sc = new Scanner(System.in);
2.Buffered Reader & InputStream Reader:
---------------------------------------
BufferedReader br = new BufferedReader(new InputStreamReader(System.in))
3.System.in:
--------------
-> represents object
-->println() & print()
Arrays:
-------
-->Collection of elements of the same data type.
-->Array is used to store multiple values in single variable.
int[] a={10,20,30,40,50};
int[] a=new int[5];
a[0]=10;
a[1]=20;
a[2]=30;
a[3]=40;
a[4]=50'
System.out.println(a[2]);
--> dynamically allocated
--> Size of an array changed at runtime
--> Once size of array is declared can't be changed.
String[] names = {"John","Shiva"};
Enhanced for loop:
------------------
for-each loop
Ex:
----
int[] numbers = {10,20,30,40};
for(int number:numbers){
S.O.Pln(number);
}
Ex:
===
Multi-Dimensional Array:
------------------------
--> array of arrays.
--> int[][] twoDimensionalArray = new int[3][3];
//initialize the values
------------------------
for(int i=0;i<3;i++){//0,1,2
for(int j=0;j<3;j++){//0,1,2
twoDimensionalArray[i][j]=i+j;
}
}
Jagged Arrays:
--------------
--> array of arrays,where each sub-array can have different length.
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[4];
jaggedArray[1] = new int[5];
jaggedArray[2] = new int[6];
jaggedArray[0][0]= 1;
jaggedArray[1][1]= 2;
var-args:
---------
--> indefinite number of arguments
--> Using ...(Spread Operator)
Ex:
---
public static int sum(int...numbers)
{
int sum = 0;
for(int number:numbers){
sum+=number;
}
return sum;
}
int result = sum(1,2,3,4,5);
Method Passing Arrays:
----------------------
public static void main(String...args){
int[] array = [1,2,3,4,5];
printArray(array);
}
public static void printArray(int[] array){
for(int i=0;i<array.length;i++){
s.o.pln(array[i])
}
}
Download