8/5/2025
ISBM College of Engineering
Department of Computer Engineering
OOP & CG
Unit I – Introduction to OOP Concepts
and Control Structure
By
Prof. Gajanan Bhusare
Unit I Contents
• Programming paradigms - Introduction to programming paradigms, Introduction
to four main Programming paradigms- procedural, object oriented, functional, and
logic & rule based. Need of object- oriented programming,
• Fundamentals of object-oriented programming: Namespaces, objects, classes,
data members, methods, messages, data encapsulation, data abstraction and
information hiding, inheritance, polymorphism. Benefits of OOP, Java as object
oriented programming language.
• Overview of Java Language: simple Java program structure: documentation
section, package statement, import statements, class definition, main method
class. Implementing Java Program, , Data types, Primitive Types vs. Reference
type, floating point numbers, operators and expressions, Java Class Libraries,
Typical Java Development Environment, and Memory Concepts.
• Control Statements: Selection Statements: if, if-else, nested if-else, Iteration
Statements: do, while, for, for-each statement, break, and continue statements
Unit I - Prof. Gajanan Bhusare 2
1
8/5/2025
PROGRAMMING PARADIMES
Unit I - Prof. Gajanan Bhusare 3
Introduction to programming paradigms
• A paradigm can also be termed as a method to solve a problem or
accomplish a task
• What is a Programming Paradigm?
• A programming paradigm is an approach to solving a problem using a
specific programming language.
• It represents a way of thinking about software construction, organizing
code, and solving problems using particular techniques and principles.
• These paradigms define the structure and style of programming
Unit I - Prof. Gajanan Bhusare 4
2
8/5/2025
Introduction to four main Programming
paradigms-
1. Procedural Programming
• Writing step-by-step instructions, known as procedures or functions, to
perform tasks.
• The program follows a structured approach where code is executed in a
sequence.
• Key Features of Procedural Programming:
• Uses functions to divide a program into smaller, reusable parts.
• Follows a top-down approach where tasks are executed in order.
• Uses variables, loops, and conditional statements to control program flow.
• Examples of procedural languages: C, Pascal, and BASIC.
Unit I - Prof. Gajanan Bhusare 5
• Advantages:
• Simple and easy to understand.
• Efficient for small to medium-sized applications.
• Code can be reused using functions, reducing redundancy.
• Disadvantages:
• Becomes difficult to manage for large programs.
• Less secure due to the use of global variables.
• Code modification can be complex if changes are required.
• Procedural programming is widely used in applications like operating
systems, embedded systems, and basic software development.
Unit I - Prof. Gajanan Bhusare 6
3
8/5/2025
2. Object-Oriented Programming (OOP)
• It is a paradigm that organizes code into objects.
• An object is a self-contained unit that contains data (attributes) and
behavior (methods).
• OOP allows developers to design software in a more structured and
reusable manner.
• Key Features of OOP:
• Uses classes as blueprints to create objects.
• Supports encapsulation, which hides details and protects data.
• Uses inheritance, allowing new classes to reuse existing code.
• Implements polymorphism, enabling objects to take multiple forms.
• Examples of OOP languages: Java, C++, and Python.
Unit I - Prof. Gajanan Bhusare 7
• Advantages:
• Makes code reusable and modular.
• Easier to manage large and complex programs.
• Increases security by controlling data access.
• Disadvantages:
• Requires more memory and processing power.
• Can be complex for beginners to understand.
• Debugging can be difficult due to interdependencies.
• OOP is commonly used in software development, including mobile apps,
web applications, and game development.
Unit I - Prof. Gajanan Bhusare 8
4
8/5/2025
3. Functional Programming
• It is based on treating functions as the main building blocks of a program.
• It avoids changing data and relies on mathematical functions to perform
tasks.
• Key Features of Functional Programming:
• Uses pure functions that always return the same output for the same
input.
• Avoids mutability, meaning data does not change once created.
• Supports higher-order functions, which can take other functions as
inputs.
• Uses recursion instead of loops for iteration.
• Examples of functional languages: Haskell, Lisp, and Scala.
Unit I - Prof. Gajanan Bhusare 9
• Advantages:
• Makes code easier to test and debug.
• Encourages modular and reusable code.
• Reduces unexpected errors by avoiding data modification.
• Disadvantages:
• Requires a different way of thinking compared to traditional paradigms.
• Can be less efficient for certain tasks.
• Not all languages fully support functional programming.
• Functional programming is widely used in data analysis, artificial
intelligence, and financial applications.
Unit I - Prof. Gajanan Bhusare 10
5
8/5/2025
4. Logic & Rule Based Paradigm
• Logic Based:
• used to implement aspects of logic-based programming.
• focuses on expressing knowledge and rules, and using those to derive new
information through reasoning.
• Rule Based :
• Paradigm where the program's behavior is determined by a set of rules that
define relationships between different entities and how the program should
respond to various situations.
Unit I - Prof. Gajanan Bhusare 11
Need Of Object- Oriented Programming
• Provides a structured way to organize and manage complex codebases.
• Making software more modular, reusable, and maintainable, by
encapsulating data and behavior into objects, which interact with each
other to perform tasks.
• Managing Complexity:
• Code Reusability:
• Data Security:
• Real-World Modeling:
• Flexibility and Scalability:
Unit I - Prof. Gajanan Bhusare 12
6
8/5/2025
Need Of Object- Oriented Programming
• Managing Complexity:
• Large and complex software can be difficult to understand and maintain.
• OOP helps by breaking down the system into smaller, manageable objects,
each with its own data and methods (functions that operate on the data).
• This modularity makes it easier to develop, debug, and understand
individual components, leading to a more organized and maintainable
codebase.
Unit I - Prof. Gajanan Bhusare 13
Need Of Object- Oriented Programming
• Code Reusability:
• OOP promotes code reuse through concepts like inheritance, where
new classes can inherit properties and behaviors from existing ones.
• This reduces redundancy and development time, as you don't have to
rewrite code for similar functionalities.
Unit I - Prof. Gajanan Bhusare 14
7
8/5/2025
Need Of Object- Oriented Programming
• Data Security:
• OOP allows for encapsulation, where data is bundled within an object and
protected from outside access.
• Only the methods of the object can interact with its data, preventing
accidental or unauthorized modification.
Unit I - Prof. Gajanan Bhusare 15
Need Of Object- Oriented Programming
• Real-World Modeling:
• OOP allows for the modeling of real-world entities as objects, making
it easier to represent and simulate complex systems.
• This natural mapping between the real world and the code can
simplify the development process and improve understanding.
Unit I - Prof. Gajanan Bhusare 16
8
8/5/2025
Need Of Object- Oriented Programming
• Flexibility and Scalability:
• OOP provides flexibility through inheritance and polymorphism,
allowing for the creation of versatile and adaptable systems.
• As the software evolves, OOP makes it easier to add new features or
modify existing ones without affecting other parts of the system.
Unit I - Prof. Gajanan Bhusare 17
FUNDAMENTALS OF
OBJECT ORIENTED PROGRAMMING
Unit I - Prof. Gajanan Bhusare 18
9
8/5/2025
Namespaces
• Namespaces provide a method for preventing name conflicts in large
projects.
• Symbols declared inside a namespace block are placed in a named
scope that prevents them from being mistaken for identically-named
symbols in other scopes.
• Multiple namespace blocks with the same name are allowed. All
declarations within those blocks are declared in the named scope.
Unit I - Prof. Gajanan Bhusare 19
Objects
• The fundamental building blocks, representing instances of classes that
encapsulate data (attributes) and behavior (methods)
• An object is a real-world entity that has a particular behavior and a state.
• It can be physical or logical programming language.
• An object is an instance of a class and memory is allocated only when an
object of the class is created.
Unit I - Prof. Gajanan Bhusare 20
10
8/5/2025
Classes
• Class acts as a blueprint for creating objects.
• It defines the structure and behavior that objects of that class will have.
• Essentially, a class specifies the data (attributes) and functions (methods)
that an object will possess.
• Collection of objects is called class. It is a logical entity.
• A class instance must be created in order to access and use the user-
defined data type's data members and member functions.
Unit I - Prof. Gajanan Bhusare 21
Data Members
• Data member refers to a variable that is a part of a class.
• These variables hold data associated with objects of the class.
• Data members can be of various types, including fundamental data types
(such as int, float, double, etc.), user-defined types (such as other
classes), or even arrays.
• Data members are typically declared within the body of a class.
• They can have different access specifiers like public, private, or protected,
which determine their visibility and accessibility from outside the class
Unit I - Prof. Gajanan Bhusare 22
11
8/5/2025
Methods
• A method is a collection of statements that perform a specific task.
• Method names typically begin with a lowercase letter.
• It provides the reusability of code as we write a method once and use
it many times by invoking the method.
• The most important method is Java's main() method.
Unit I - Prof. Gajanan Bhusare 23
Messages
• Objects communicate with one another by sending and receiving information
from each other.
• Message passing involves specifying the name of the object, the name of the
function, and the information to be sent.
• messages are requests sent between objects to trigger specific actions or retrieve
information.
• Think of it as objects communicating with each other to get things done.
• A message includes the receiving object, the action to be performed (a method),
and any necessary data for that action.
Unit I - Prof. Gajanan Bhusare 24
12
8/5/2025
Unit I - Prof. Gajanan Bhusare 25
Data Encapsulation
• Encapsulation helps to wrap up the functions and data together in a single unit.
• By privatizing the scope of the data members it can be achieved.
• This particular feature makes the program inaccessible to the outside world.
• It also involves restricting direct access to some of the object's components,
often through access modifiers like private, protected, and public. This helps in
hiding the internal implementation details of a class and exposing only what is
necessary to the outside world
Unit I - Prof. Gajanan Bhusare 26
13
8/5/2025
Unit I - Prof. Gajanan Bhusare 27
Data Abstraction
• Hiding internal details and showing functionality is known as abstraction.
• Data abstraction is the process of exposing to the outside world only the
information that is absolutely necessary while concealing
implementation or background information.
• We use abstract class and interface to achieve abstraction.
Unit I - Prof. Gajanan Bhusare 28
14
8/5/2025
Unit I - Prof. Gajanan Bhusare 29
Information Hiding
• Data hiding means hiding the internal data within the class to prevent
its direct access from outside the class.
Access Specifiers
• Data hiding involves classes, and classes have three different kinds of
protection/access specifiers.
• Typically, the data within a class is private, and the functions are
public. Since the data is hidden, it will be safe from accidental
manipulation.
Unit I - Prof. Gajanan Bhusare 30
15
8/5/2025
• Private members/methods: Functions and variables declared as private
can be accessed only within the same class, and they cannot be accessed
outside the class they are declared.
• Public members/methods: Functions and variables declared under public
can be accessed from anywhere.
• Protected members/methods: Functions and variables declared as
protected cannot be accessed outside the class except a child class. This
specifier is generally used in inheritance.
Unit I - Prof. Gajanan Bhusare 31
Inheritance
• When one object acquires all the properties and behaviors of parent
object i.e. known as inheritance.
• It provides code reusability. It is used to achieve runtime polymorphism.
• Sub class - Subclass or Derived Class refers to a class that receives
properties from another class.
• Super class - The term "Base Class" or "Super Class" refers to the class
from which a subclass inherits its properties.
Unit I - Prof. Gajanan Bhusare 32
16
8/5/2025
• Reusability - As a result, when we wish to create a new class, but an
existing class already contains some of the code we need, we can
generate our new class from the old class.
• This allows us to utilize the fields and methods of the pre-existing class.
Unit I - Prof. Gajanan Bhusare 33
Unit I - Prof. Gajanan Bhusare 34
17
8/5/2025
Polymorphism
• Polymorphism means the ability to take more than one form in the
programming language.
• With this feature, you can use the same function to perform different
tasks thus increasing code reusability.
Unit I - Prof. Gajanan Bhusare 35
Unit I - Prof. Gajanan Bhusare 36
18
8/5/2025
• 1. Method Overloading: Also, known as compile-time polymorphism,
is the concept of Polymorphism where more than one method share
the same name with different signature(Parameters) in a class. The
return type of these methods can or cannot be same.
• 2. Method Overriding: Also, known as run-time polymorphism, is the
concept of Polymorphism where method in the child class has the
same name, return-type and parameters as in parent class. The child
class provides the implementation in the method already written.
Unit I - Prof. Gajanan Bhusare 37
Benefits of OOP
• Modular, scalable, extensible, reusable, and maintainable.
• It models the complex problem in a simple structure.
• Object can be used across the program.
• Code can be reused.
• We can easily modify, append code without affecting the other code
blocs.
• Provides security through encapsulation and data hiding features.
• Beneficial to collaborative development in which a large project is
divided into groups.
• Debugging is easy.
Unit I - Prof. Gajanan Bhusare 38
19
8/5/2025
Limitations of OOP
• Requires intensive testing processes.
• Not suitable for small problems.
• Takes more time to solve problems.
• programs can consume a large amount of memory.
• The size of the programs created using this approach may become larger than
the programs written using the procedure-oriented programming approach.
• Software developed using this approach requires a substantial amount of pre-
work and planning.
• OOP code is difficult to understand if you do not have the corresponding class
documentation.
Unit I - Prof. Gajanan Bhusare 39
Java As Object Oriented Programming Language
• Class - user-defined blueprint or prototype from which objects are
created
• Object - a basic unit of Object-Oriented Programming that represents
real-life entities.
• Inheritance - one class is allowed to inherit the features (fields and
methods) of another class.
• Polymorphism - to differentiate between entities with the same
name efficiently.
• Abstraction - only the essential details are displayed to the user.
• Encapsulation - wrapping up of data under a single unit
Unit I - Prof. Gajanan Bhusare 40
20
8/5/2025
Unit I - Prof. Gajanan Bhusare 41
OVERVIEW OF JAVA LANGUAGE
Unit I - Prof. Gajanan Bhusare 42
21
8/5/2025
Overview of Java Language:
• Java is a high-level, object-oriented programming language developed
by Sun Microsystems in 1995.
• It is platform-independent, which means we can write code once and
run it anywhere using the Java Virtual Machine (JVM).
• Java is mostly used for building desktop applications, web applications,
Android apps, and enterprise systems.
• Java is case-sensitive, platform-independent, and uses both compiler
and interpreter.
Unit I - Prof. Gajanan Bhusare 43
•Key Features of Java
• Platform Independent: Java is famous for its Write Once, Run Anywhere
(WORA) feature. This means we can write our Java code once, and it will
run on any device or operating system without changing anything.
• Object-Oriented: Java follows the object-oriented programming. This
makes code clean and reusable.
• Security: Java does not support pointers, it includes built-in protections
to keep our programs secure from common problems like memory
leakage.
Unit I - Prof. Gajanan Bhusare 44
22
8/5/2025
• Multithreading: Java programs can do many things at the same time using
multiple threads. This is useful for handling complex tasks like processing
transactions.
• Just-In-Time (JIT) Compiler: Java uses a JIT compiler. It improves
performance by converting the bytecode into machine readable code at
the time of execution.
Unit I - Prof. Gajanan Bhusare 45
Simple Java Program Structure:
Unit I - Prof. Gajanan Bhusare 46
23
8/5/2025
Documentation Section
• details about the program includes the author's name, creation date,
version, program name, company name, and a brief description
• optional part of a Java program,
• To include these details, programmers typically use comments.
//Single-line comment
/* Multiline comment
in Java */
/** Documentation comment */
Unit I - Prof. Gajanan Bhusare 47
Package Statement
• Declaring the package in the structure of Java is optional.
• It comes right after the documentation section.
• You mention the package name where the class belongs.
• Only one package statement is allowed in a Java program and must come
before any class or interface declaration.
• This declaration helps organize classes into different directories based on
the modules they're used in.
• You use the keyword package followed by the package name.
• package scaler; //scaler is the package name
• package com.scaler; //com is the root directory, and scaler is the
subdirectory
Unit I - Prof. Gajanan Bhusare 48
24
8/5/2025
import statements
• Import statements are used to import classes, interfaces, or enums that
are stored in packages or the entire package.
• A package contains many predefined classes and interfaces.
• We need to mention which package we are using at the beginning of the
program.
• We do it by using the import keyword.
• We either import the entire package or a specific class from that
package.
Unit I - Prof. Gajanan Bhusare 49
Interface Section
• This is an optional section.
• The keyword interface is used to create an interface.
• An interface comprises a set of cohesive methods that lack
implementation details., i.e. method declaration and constants.
Example :
interface Code {
void write();
void debug();
}
Unit I - Prof. Gajanan Bhusare 50
25
8/5/2025
Class Definition
• This is a mandatory section in the structure of Java program.
• Each Java program has to be written inside a class as it is one of the main
principles of Object-oriented programming that Java strictly follows, i.e.,
its Encapsulation for data security.
• There can be multiple classes in a program. Some conventions need to be
followed to name a class. They should begin with an uppercase letter.
class Program{
// class definition
}
Unit I - Prof. Gajanan Bhusare 51
Class Variables and Variables
• Identifiers are used to name classes, methods, and variables.
• It can be a sequence of uppercase and lowercase characters.
• It can also contain '_' (underscore) and '$' (dollar) signs.
• It should not start with a digit(0-9) and not contain any special characters.
• Variables are also known as identifiers in Java. It is a named memory
location which contains a value.
• In a single statement, we're able to declare multiple variables of the same
type
int var=100;
int g;
char c,d; // declaring more than one variable in a statement
Unit I - Prof. Gajanan Bhusare 52
26
8/5/2025
main method class
• This is a compulsory part of the structure of Java program.
• This is the entry point of the compiler where the execution starts.
• It is called/invoked by the Java Virtual Machine or JVM.
• The main() method should be defined inside a class.
• We can call other functions and create objects using this method.
public static void main(String[] args) {
// Method logic
}
Unit I - Prof. Gajanan Bhusare 53
Implementing Java Program
import java.io.*;
public class Main{
public static void main(String[] args) throws Exception {
System.out.println("Hello, Java!");
}
Unit I - Prof. Gajanan Bhusare 54
27
8/5/2025
Data Types
Unit I - Prof. Gajanan Bhusare 55
Primitive Data Types Table
Data Type Default Value Default size Range
byte 0 1 byte or 8 bits -128 to 127
short 0 2 bytes or 16 bits -32,768 to 32,767
int 0 4 bytes or 32 bits 2,147,483,648 to 2,147,483,647
9,223,372,036,854,775,808 to
long 0 8 bytes or 64 bits
9,223,372,036,854,775,807
float 0.0f 4 bytes or 32 bits 1.4e-045 to 3.4e+038
double 0.0d 8 bytes or 64 bits 4.9e-324 to 1.8e+308
char ‘u0000’ 2 bytes or 16 bits 0 to 65536
boolean FALSE 1 byte or 2 bytes 0 or 1
Unit I - Prof. Gajanan Bhusare 56
28
8/5/2025
Primitive Types Vs. Reference Type
Feature Primitive Type Reference Type
Storage Direct value Memory address (reference)
Location Stack (usually) Heap (usually)
Pass by Value Reference
Size Fixed Varibale
Mutability Often immutable Often mutable
Unit I - Prof. Gajanan Bhusare 57
Primitive Types:
• Direct Storage:
• store the actual data value directly within the variable itself.
• Examples:
• In many languages, these include int, float, double, char, boolean, and
similar basic data types.
• Stack Allocation:
• Primitive types are typically stored on the stack, a region of memory that
is fast to access but limited in size.
Unit I - Prof. Gajanan Bhusare 58
29
8/5/2025
Primitive Types: Cont..
• Value Semantics:
• When passed to a function, a copy of the primitive value is created,
so changes within the function do not affect the original value.
• Immutable:
• Primitive types are often immutable, meaning their values cannot be
changed after they are created.
Unit I - Prof. Gajanan Bhusare 59
Reference Types:
• Indirect Storage:
• Reference types store a reference (memory address) to the actual data,
which may be stored elsewhere in memory.
• Examples:
• In many languages, these include objects, arrays, strings (in some
languages), and other complex data structures.
• Heap Allocation:
• Reference types are typically stored on the heap, a larger region of
memory that is slower to access but can hold more data.
Unit I - Prof. Gajanan Bhusare 60
30
8/5/2025
Reference Types: Cont…
• Reference Semantics:
• When passed to a function, the reference (memory address) is
passed, so changes made through the reference affect the original
data.
• Mutable:
• Reference types are often mutable, meaning their internal state can
be modified after they are created.
Unit I - Prof. Gajanan Bhusare 61
Floating Point Numbers
• To represent values with fractional components, often referred to as real
numbers.
• float:
• This is a single-precision 32-bit IEEE 754 floating-point type.
• It uses 4 bytes of memory and has a default value of 0.0f.
• When assigning a literal value to a float, it must be suffixed with f or F
(e.g., float pi = 3.14f;).
• double:
• This is a double-precision 64-bit IEEE 754 floating-point type.
• It uses 8 bytes of memory and offers greater precision than float.
• It is the default type for floating-point literals in Java (e.g., double price =
19.99;).
Unit I - Prof. Gajanan Bhusare 62
31
8/5/2025
Operators And Expressions
• Operators are special symbols that perform specific operations on
one or more operands (variables or values).
• Expressions are constructs formed by combining operators and
operands, which evaluate to a single value.
Unit I - Prof. Gajanan Bhusare 63
Types of Operators in Java
1. Arithmetic Operators
2. Unary Operators
3. Assignment Operator
4. Relational Operators
5. Logical Operators
6. Ternary Operator
7. Bitwise Operators
8. Shift Operators
9. instance of operator
Unit I - Prof. Gajanan Bhusare 64
32
8/5/2025
Unit I - Prof. Gajanan Bhusare 65
Java Class Libraries
• The Java Class Library (JCL), also known as the Java Standard Library or Java
API,
• is a comprehensive collection of pre-built classes and interfaces provided as
part of the Java Development Kit (JDK).
• These libraries offer a wide range of functionalities to assist developers in
building Java applications efficiently.
• Pre-defined Classes and Interfaces:
• The JCL consists of thousands of pre-written classes and interfaces that
provide ready-to-use solutions for common programming tasks.
Unit I - Prof. Gajanan Bhusare 66
33
8/5/2025
Java Class Libraries Cont…
• Organized into Packages:
• These classes are logically grouped into packages, such as java.lang,
java.util, java.io, java.net, etc., for better organization and management.
• Core Functionality:
• The libraries cover essential areas like input/output operations, data
structures, networking, security, database connectivity, and graphical user
interface (GUI) development.
Unit I - Prof. Gajanan Bhusare 67
Java Class Libraries Cont…
• Automatic Import:
• The java.lang package, containing fundamental classes like Object,
String, and Math, is automatically imported into every Java program.
Other packages require explicit import statements.
• Simplifying Development:
• By providing reusable components and functions, the JCL significantly
simplifies and accelerates application development, reducing the need
to write code from scratch for common tasks.
Unit I - Prof. Gajanan Bhusare 68
34
8/5/2025
Examples of commonly used packages:
• java.lang: Fundamental classes, including String, Math, System, and Object.
• java.util: Utility classes for data structures (e.g., ArrayList, HashMap),
date/time handling, and more.
• java.io: Classes for input and output operations, such as file handling and
streams.
• java.net: Classes for network programming, including sockets and URLs.
• java.sql: Classes for interacting with databases.
• java.awt and javax.swing: Classes for creating graphical user interfaces
Unit I - Prof. Gajanan Bhusare 69
Typical Java Development Environment, and
Memory Concepts.
• A typical Java development environment includes the Java Development
Kit (JDK), which provides tools for compiling and running Java code, and
an Integrated Development Environment (IDE) like Eclipse, IntelliJ IDEA,
or NetBeans, which offers features like code completion, debugging, and
project management.
• Java's memory management is handled by the Java Virtual Machine
(JVM), which allocates memory for objects in the heap and for method
calls and local variables in the stack.
Unit I - Prof. Gajanan Bhusare 70
35
8/5/2025
Java Development Environment:
• Java Development Kit (JDK):
• The JDK is a software development environment that includes tools like
the Java compiler (javac), the Java runtime (java), and the JAR archiver
(jar).
• It provides everything needed to compile, debug, and run Java programs.
Unit I - Prof. Gajanan Bhusare 71
• Integrated Development Environment (IDE):
• An IDE is a software application that provides comprehensive facilities to
computer programmers for software development. Popular Java IDEs
include:
• Eclipse: Known for its flexibility and extensive plugin ecosystem.
• IntelliJ IDEA: Praised for its intelligent code completion and refactoring tools.
• NetBeans: A free and open-source IDE that is easy to use and has good support
for web and enterprise development.
Unit I - Prof. Gajanan Bhusare 72
36
8/5/2025
Java Memory Concepts:
• Java Virtual Machine (JVM):
• The JVM is a key component of the Java environment that provides a
platform-independent runtime environment. It manages memory
allocation and deallocation for Java applications.
• Heap:
• The heap is a region of memory where Java objects are allocated.
When you create a new object using the new keyword, it's stored in
the heap.
• Stack:
• The stack is another region of memory used to store method calls,
local variables, and references to objects. Each thread in a Java
program has its own stack.
Unit I - Prof. Gajanan Bhusare 73
Java Memory Concepts: Cont…
• Garbage Collection:
• The JVM includes a garbage collector that automatically reclaims memory
occupied by objects that are no longer in use. This process helps prevent
memory leaks and improves overall memory management.
• Method Area/Metaspace:
• In addition to the heap and stack, the JVM also has a method area (or
metaspace in more recent JVM versions) that stores class-level
information, such as class structures, method bytecode, static variables,
and the constant pool. Unit I - Prof. Gajanan Bhusare 74
37
8/5/2025
CONTROL STATEMENTS
Unit I - Prof. Gajanan Bhusare 75
Control Statements: Selection Statements: if
• the if statement is a fundamental selection control statement used to
execute a block of code conditionally. It allows a program to make
decisions and execute specific code segments only when a given
condition evaluates to true
if (condition)
{
// Code to be executed if the condition is true
}
Unit I - Prof. Gajanan Bhusare 76
38
8/5/2025
if-else
• the if-else statement is a selection control statement used to execute different
blocks of code based on a condition. It allows a program to make decisions and
follow different paths of execution.
if (condition)
{
// Code to be executed if the condition is true
} else {
// Code to be executed if the condition is false
}
Unit I - Prof. Gajanan Bhusare 77
nested if-else
• a nested if-else statement involves placing an if or if-else block inside
another if or else block.
• This allows for hierarchical decision-making, where a condition is
evaluated, and if it's true (or false, for the else branch), another
condition is then evaluated within that specific branch.
Unit I - Prof. Gajanan Bhusare 78
39
8/5/2025
if (outerCondition) {
// Code to execute if outerCondition is true
if (innerCondition1) {
// Code to execute if outerCondition and innerCondition1 are true
} else {
// Code to execute if outerCondition is true but innerCondition1 is false
}
} else {
// Code to execute if outerCondition is false
if (innerCondition2) {
// Code to execute if outerCondition is false and innerCondition2 is true
} else {
// Code to execute if outerCondition is false and innerCondition2 is false
}
}
Unit I - Prof. Gajanan Bhusare 79
Iteration Statements: do while
• the do-while loop is an iteration statement that guarantees the
execution of its code block at least once before evaluating the loop
condition.
do {
// Statements to be executed
} while (condition);
Unit I - Prof. Gajanan Bhusare 80
40
8/5/2025
for
• The for loop in Java is an iteration statement used to repeatedly execute
a block of code a specific number of times. It is particularly useful when
the number of iterations is known beforehand.
for (initialization; condition; increment/decrement) {
// statements to be executed repeatedly
}
Unit I - Prof. Gajanan Bhusare 81
for-each statement
• The for-each statement, also known as the enhanced for loop, in Java
provides a simplified way to iterate over elements of arrays and collections
(like ArrayList, HashSet, etc.).
• It was introduced in Java 5 to make code more readable and concise when the
need is to simply access each element in a sequence without needing an
explicit index.
for (DataType element : collection Or Array) {
// Code to be executed for each element
}
Unit I - Prof. Gajanan Bhusare 82
41
8/5/2025
• Explanation:
• DataType: This represents the data type of the elements within the
collection Or Array.
• element: This is a variable that will sequentially hold each element
from the collection Or Array during each iteration of the loop.
• Collection Or Array: This refers to the array or collection you want to
iterate over.
• How it works:
• The for-each loop iterates through each element in the collection Or
Array from beginning to end.
• In each iteration, the current element is assigned to the element
variable, and the code inside the loop's body is executed.
Unit I - Prof. Gajanan Bhusare 83
Example
public class ForEachExample {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
// Iterating over an array using a for-each loop
for (int num : numbers) {
System.out.println(num);
}
}
}
Unit I - Prof. Gajanan Bhusare 84
42
8/5/2025
break, and continue statements
• break and continue are jump statements used within iteration statements
(loops) to alter the normal flow of execution.
• 1. break statement:
• The break statement is used to immediately terminate the innermost loop (or
switch statement) in which it is encountered.
• When break is executed, control is transferred to the statement immediately
following the loop or switch block.
• It is commonly used when a specific condition is met within a loop, and further
iterations are no longer necessary.
Unit I - Prof. Gajanan Bhusare 85
Example of Break Statement
for (int i = 0; i < 10; i++) {
if (i == 5) {
break; // Exit the loop when i is 5
}
System.out.println(i);
}
// Output: 0, 1, 2, 3, 4
Unit I - Prof. Gajanan Bhusare 86
43
8/5/2025
• 2. continue statement:
• The continue statement is used to skip the current iteration of a loop
and proceed to the next iteration.
• When continue is executed, the remaining statements within the
current iteration of the loop are bypassed, and the loop's control
expression is re-evaluated for the next iteration.
• It is useful when certain conditions within a loop require skipping
specific processing steps for the current iteration, but the loop needs
to continue.
Unit I - Prof. Gajanan Bhusare 87
Example of Continue Statement
for (int i = 0; i < 5; i++) {
if (i == 2) {
continue; // Skip the current iteration when i is 2
}
System.out.println(i);
}
// Output: 0, 1, 3, 4
Unit I - Prof. Gajanan Bhusare 88
44
8/5/2025
THANK YOU !
Unit I - Prof. Gajanan Bhusare 89
45