Module 1
Module 1
MODULE 1
Prepared By
Prof. Bhagya S G
AI&ML Dept. ,
BIET College , Davangere
What is Java? (Brief Overview)
• Java is a high-level, object-oriented programming language developed by
Sun Microsystems.
• It is platform-independent due to its "write once, run anywhere" capability.
• Java is widely used for web, mobile, and enterprise applications.
• It features a robust standard library with built-in functions for various
tasks.
• Java uses a virtual machine (JVM) to execute bytecode across platforms.
• It emphasizes security, simplicity, and performance in software
development.
History and Evolution of Java
• Java was developed by James Gosling at Sun Microsystems in the early
1990s.
• Initially called "Oak," it was renamed "Java" in 1995 due to trademark issues.
• Java was released publicly in 1996, with version 1.0 marking its first major
release.
• Sun Microsystems was acquired by Oracle in 2010, which continues to
develop Java.
• Java has evolved through several versions, adding new features with each
update.
• Its widespread adoption spans industries from finance to gaming and mobile
applications.
Object-Oriented Paradigm in Java
• Java is built around the Object-Oriented Programming paradigm, where everything revolves around
objects and classes.
• It promotes the organization of complex software projects using reusable and maintainable structures.
• OOP in Java allows for encapsulation, inheritance, and polymorphism, providing flexibility in
designing applications.
• Objects are instances of classes, which define data attributes and methods to operate on them.
• It allows modeling of real-world entities and relationships, simplifying complex problem-solving
processes.
• Java's OOP paradigm enables better collaboration, as objects interact through clearly defined interfaces.
Two Programming Paradigms (Procedural vs
Object-Oriented)
• Procedural programming focuses on sequences of instructions executed step by step to solve a problem.
• Object-oriented programming (OOP) emphasizes the creation of objects representing real-world entities,
improving modularity.
• In procedural programming, functions are used to perform operations, while OOP uses methods inside
objects.
• OOP promotes code reusability through inheritance, unlike procedural programming's repetitive code
structures.
• Procedural programming offers simplicity for small tasks, while OOP better manages complex, large-
scale applications.
• Java's object-oriented nature provides flexibility and modular design compared to the linear approach of
procedural programming.
Abstraction in Java
• Abstraction in Java hides implementation details, exposing only essential features to simplify
software design.
• Java supports abstraction through abstract classes and interfaces, defining common behaviors
without specifying details.
• By using abstraction, developers can work with high-level functionalities without worrying about
underlying complexity.
• Abstraction allows for code modification without affecting other parts of the system, enhancing
flexibility.
• Java's abstraction enhances security by restricting access to internal data and methods of classes.
• The concept of abstraction promotes the design of user-friendly, modular, and easily understandable
software systems.
The Three OOP Principles (Encapsulation,
Inheritance, Polymorphism)
• Encapsulation binds data and methods together within a class, ensuring controlled access using access
modifiers.
• Inheritance allows a new class to acquire properties and behaviors of an existing class, promoting code
reuse.
• Polymorphism enables objects to take on different forms, allowing for method overriding and
overloading in Java.
• Encapsulation enhances security by restricting direct access to class data, enforcing proper usage of
class methods.
• Inheritance provides hierarchical structures, allowing for shared functionalities and specialized
behaviors.
• Polymorphism offers flexibility in handling different data types and method signatures using a unified
interface.
Examples of OOP Concepts in Java
• Encapsulation example: A bank account class encapsulates account number, balance, and methods for
deposit and withdrawal.
• Inheritance example: A subclass "Dog" inherits properties from a superclass "Animal," adding unique
behaviors.
• Polymorphism example: A superclass reference can point to subclass objects, allowing method
overriding in Java.
• Abstract class example: A shape class with abstract methods like draw(), implemented differently by
subclasses.
• Interface example: A class implements an interface "Flyable," defining methods for flight without
specifying how.
• Real-world example: A car class with properties like model, speed, and methods to accelerate and brake.
Blocks of Code and Lexical
Issues
Introduction to Blocks of Code (Syntax and
Use)
• A block in Java is a group of statements enclosed in curly braces `{}`.
{
int x = 10;
System.out.println(x);
}
• Identifiers are names for variables, methods, and classes, starting with
a letter or underscore. int myVar = 10;
String _name = "Java";
• '2D arrays' are grid structures, storing data in rows and columns like a
matrix. int[][] matrix = {{1, 2}, {3, 4}};
• These examples showcase arrays for structured data and type inference
for simplifying variable declarations.
Arithmetic Operators (Examples)
• Arithmetic operators perform basic mathematical operations like addition,
subtraction, multiplication, and division.
• Common operators include '+' for addition, '-' for subtraction, '*' for
multiplication, and '/' for division.
• The '%' operator calculates the remainder of a division, known as the modulus
operator.
• Unary operators like '++' (increment) and '--' (decrement) change the value of
variables by one.
• Arithmetic operators can be used
int result = 10 +with both10integers
5; // Adds and
and 5, result floating-point values.
is 15
• These operators are commonly used for calculations in loops and functions.
Relational Operators and Boolean Logical
Operators
• Relational operators compare two values and return 'true' or 'false' as a result.
• Common relational operators include '==' (equal to), '!=' (not equal to), '>' (greater
than), and '<' (less than).
• Boolean logical operators include '&&' (AND), '||' (OR), and '!' (NOT).
• '&&' evaluates to 'true' only if both operands are true, while '||' evaluates to 'true' if
either operand is true.
• '!' reverses the boolean value, turning true to false and vice versa.
• These operators are used in control flow statements like 'if' and 'while'.
Assignment Operator and Conditional (?
Operator)
• The assignment operator '=' assigns a value to a variable.
int x = 5; // Assigns 5 to variable x
• The left side of '=' is the variable, and the right side is the value being assigned.
• The '? :' operator evaluates a condition and returns one of two values depending
on the result.
• Assignment and conditional operators are frequently used in loops and decision-
making logic.
Operator Precedence and Parentheses Usage
• Operator precedence defines the order in which operators are evaluated in an expression.
• Operators like '*' and '/' have higher precedence than '+' and '-', meaning they are
executed first.
• Parentheses '()' can be used to override default precedence and force specific
evaluations.
int result = (2 + 3) * 4; // Parentheses ensure addition happens
before multiplication
• The 'for' loop includes initialization, condition, and iteration expressions for
compact syntax. for (int i = 0; i < 5; i++) {
System.out.println(i);
}
• These loops automate repetitive tasks like processing data and performing
calculations.
For-Each Loop and Type Inference in Loops
• The 'for-each' loop simplifies iteration over arrays and collections,
automatically handling each element.
• It eliminates the need to manage loop counters and indices explicitly.
for (String name : names) {
System.out.println(name);
}
• 'for-each' improves code readability and reduces errors when iterating over
collections.
Using break Statement in Loops
• The 'break' statement is used to exit a loop immediately, regardless of the iteration.
• When 'break' is encountered, control jumps out of the loop and continues after it.
• Typically used to terminate a loop early based on a specific condition.
for (int i = 0; i < 10; i++) {
if (i == 5) {
break; // Loop terminates when i equals 5
}
System.out.println(i);
}
• It is commonly used in both 'for' and 'while' loops for conditional exits.
• 'break' is also used in 'switch' statements to exit a particular case block.
Using continue Statement
• The 'continue' statement skips the current iteration of a loop and moves to the next
iteration.
• When 'continue' is encountered, the remaining code inside the loop is not executed for
that iteration.
for (int i = 0; i < 10; i++) {
if (i == 5) {
continue; // Skips the rest of the code for i = 5
}
System.out.println(i);
}
• It is useful when you want to skip certain values but continue looping.
• 'continue' works with 'for', 'while', and 'do-while' loops to control flow within iterations.
The return Statement
• The 'return' statement exits a method and optionally returns a value to
the calling method.
• In 'void' methods, 'return' simply exits the method without any value.
int add(int a, int b) {
return a + b; // Returns the sum of a and b
}
• These jump statements provide control over the flow of loops and methods in Java.
Understanding Nested Loops
• Nested loops are loops inside other loops, allowing multiple levels of iteration.
• The outer loop controls the number of complete iterations of the inner loop.
• The inner loop completes all its iterations for each iteration of the outer loop.
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 2; j++) {
System.out.println("i: " + i + ", j: " + j);
}
}