3.0 Basics of Java
3.0 Basics of Java
IFTEKHARUL ABEDEEN
Contents
● Control Statements
● Arrays
● Scope of variable
● Reference variable
● Pass by Value / Reference
● Garbage collection
Control Statements
Decision making Loop
● If ● while
● Switch ● do while
● for
● for-each
Jump
● Continue
● Break
Refresher
1. if(condition){ 1. if(condition1){
3. } true
3. }else if(condition2){
4. //code to be executed if condition2 is
true
5. }
6. else if(condition3){
7. //code to be executed if condition3 is
true
8. }
9. ...
10. else{
11. //code to be executed if all the
conditions are false
12. }
switch
1. switch(expression){
2. case value1:
3. //code to be executed;
4. break; //optional
5. case value2:
6. //code to be executed;
7. break; //optional
8. ......
9. default:
10. code to be executed if all cases
are not matched;
11. }
Loops
02
while
1. class ForEachExample1{
2. public static void main(String args[]){
3. //declaring an array
4. int arr[]={12,13,14,44};
5. //traversing the array with for-each loop
6. for(int i:arr){
7. System.out.println(i);
8. }
9. }
10. }
Jump
03
break
● Random access
Disadvantages
● Size limitation (Collection can be used instead)
Declaration
1. class Testarray{
2. public static void main(String args[]){
3. int a[]=new int[5];//declaration and instantiation
4. a[0]=10;//initialization
5. a[1]=20;
6. a[2]=70;
7. a[3]=40;
8. a[4]=50;
9. }}
Scope of Variables
Default Yes No No
Private No No No
Reference variable
A reference is defined as an alias for another variable.
In short, it is like giving a different name to a pre-existing variable.