0% found this document useful (0 votes)
5 views23 pages

3.0 Basics of Java

The document covers fundamental concepts of class and objects in programming, including control statements, loops, arrays, variable scope, reference variables, parameter passing, and garbage collection. It provides examples of decision-making structures like if-else and switch statements, as well as various types of loops such as while, do-while, for, and for-each. Additionally, it discusses the advantages and disadvantages of arrays, the concept of variable scope, and the importance of garbage collection in memory management.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views23 pages

3.0 Basics of Java

The document covers fundamental concepts of class and objects in programming, including control statements, loops, arrays, variable scope, reference variables, parameter passing, and garbage collection. It provides examples of decision-making structures like if-else and switch statements, as well as various types of loops such as while, do-while, for, and for-each. Additionally, it discusses the advantages and disadvantages of arrays, the concept of variable scope, and the importance of garbage collection in memory management.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Class and Objects

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

▪Integers: byte, short, int, long ● Arithmetic operators


▪Floating points: float, double ○ +, -, /, *, %
▪Characters: char
▪boolean ● Relational operators
▪void ○ ==, !=, <, >, <=, >=
▪String

Data Types Operators


Decision
making 01
Single Chain

1. if(condition){ 1. if(condition1){

2. //code to be executed 2. //code to be executed if condition1 is

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. public class WhileExample {


2. public static void main(String[] args) {
3. int i=1;
1. while (condition){
4. while(i<=10){
2. //code to be executed
5. System.out.println(i);
3. Increment / decrement statement
6. i++;
4. }
7. }
8. }
9. }
do-while

1. public class DoWhileExample {


2. public static void main(String[] args) {
3. int i=1;
1. do{
4. do{
2. //code to be executed / loop body
5. System.out.println(i);
3. //update statement
6. i++;
4. }while (condition);
7. }while(i<=10);
8. }
9. }
for

1. for(initialization; condition; increment/decrement){


2. //statement or code to be executed
3. }

1. public class ForExample {


2. public static void main(String[] args) {
3. //Code of Java for loop
4. for(int i=1;i<=10;i++){
5. System.out.println(i);
6. }
7. }
8. }
for-each

1. for(data_type variable : array | collection){


2. //body of for-each loop
3. }

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

1. public class BreakExample {


2. public static void main(String[] args) {
3. //using for loop
4. for(int i=1;i<=10;i++){
5. if(i==5){
6. //breaking the loop
7. break;
8. }
9. System.out.println(i);
10. }
11. }
12. }
continue

1. public class ContinueExample {


2. public static void main(String[] args) {
3. //for loop
4. for(int i=1;i<=10;i++){
5. if(i==5){
6. //using continue statement
7. continue;//it will skip the rest statement
8. }
9. System.out.println(i);
10. }
11. }
12. }
Java Array
Java array is an object which contains elements of a similar data type.

The contents reside in contiguous memory locations.


Advantages
● Code Optimization

● Random access

Disadvantages
● Size limitation (Collection can be used instead)
Declaration

1. dataType[] arr; (or)


2. dataType []arr; (or)
3. dataType arr[];
4. arrayRefVar=new datatype[size];

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

Modifiers Package Subclass World / Global

Public Yes Yes Yes

Protected Yes Yes No

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.

1. int i; // Declare variable I as int


2. double d; // Declare variable d as double type
3.
4. // declare reference variables for I and d
5. int& r = i;// r is reference to i
6. double& s = d;// s is reference to d
Parameter passing
● Pass by value
● Pass by reference

Java is always pass by value.


Garbage Collection
● When no references to an object exist, that object is assumed to be no
longer needed, and the memory occupied by the object can be reclaimed.
● Garbage collection only occurs sporadically (if at all) during the execution of
your program.
● As a developer or user it is not your concern.
Thank You
Let’s go to the next slide

You might also like