0% found this document useful (0 votes)
26 views

unit 11

Uploaded by

s.hemaswathi
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)
26 views

unit 11

Uploaded by

s.hemaswathi
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/ 24

UNIT I: OOP Basics

1.1 Review of Object oriented concepts:

 Object-Oriented Programming (OOP) is the fundamental programming paradigm in Java. All


Java programs are inherently object-oriented, making it essential to grasp OOP concepts before
diving into Java programming.
 OOP organizes software design around data, or objects, rather than functions and logic. An
object can be defined as a data field with unique attributes and behavior.

Two Paradigms in Programming:

1. Process-Oriented Model:
o This traditional approach focuses on sequences of tasks or operations. Programs are
conceptualized around "what is happening," with code acting on data.
o This model is effective for smaller, simpler programs. However, as programs grow in
complexity, managing them using this approach becomes difficult.
2. Object-Oriented Model:
o OOP, in contrast, organizes programs around data, represented as objects. It emphasizes
"who is being affected" rather than "what is happening."
o Objects encapsulate data and provide well-defined interfaces to interact with that data.
This shift in focus offers several organizational and maintenance advantages, especially
in complex systems.

Core Principles of OOP:

1. Abstraction:
o Abstraction is the process of hiding the complex implementation details of a system and
exposing only the necessary features. It helps in managing complexity by simplifying
how we interact with complex systems.
o Example: A car is an abstraction. We interact with it as a single object, without needing
to understand the thousands of components and systems that make it work.
o In OOP, abstraction is achieved through the use of classes and objects, which model real-
world entities with attributes (data) and behaviors (methods).
2. Encapsulation:
o Encapsulation is the technique of bundling data (attributes) and methods (functions) that
manipulate the data into a single unit, or class, and restricting access to some of the
object's components.
o Example: In a car, the gear-shift lever is the interface to the transmission system, hiding
the complex workings of the engine from the user. The driver can operate the car without
needing to know the intricacies of the transmission.
o In Java, encapsulation is implemented through access modifiers such as private,
protected, and public, which control access to the class's data and methods. By
restricting access, encapsulation protects the integrity of the data and prevents misuse.
Encapsulation: public methods can be used to protect private data.

3. Inheritance:
o Inheritance is a mechanism where a new class, known as a subclass, derives properties
and behavior (methods) from an existing class, known as a superclass.
o Example: A Golden Retriever inherits characteristics from the Dog class, which in turn
inherits from the Mammal class, forming a hierarchical classification.
o In Java, inheritance promotes code reusability, as common functionality defined in a
superclass can be inherited by multiple subclasses. It also supports polymorphism,
allowing objects of different subclasses to be treated as objects of a common superclass.

Labrador inherits the encapsulation of all its superclasses

4. Polymorphism:
o Polymorphism allows objects of different classes to be treated as objects of a common
superclass. It also allows a single method or interface to operate on objects of various
types.
o Example: A dog’s sense of smell responds differently based on what it detects, such as
food or a cat. In programming, this allows for methods to be overridden in different
subclasses, providing specific behavior while sharing a common interface.
o In Java, polymorphism is achieved through method overloading (compile-time
polymorphism) and method overriding (runtime polymorphism). This enables flexible
and maintainable code, where the same operation can behave differently on different
classes.

Integration of OOP Principles:

 When combined, Encapsulation, Inheritance, and Polymorphism provide a powerful


framework for software development.
o Encapsulation ensures that the internal implementation of classes can be modified
without affecting the external interface.
o Inheritance allows for code reuse and the creation of more specific objects based on
more general ones.
o Polymorphism enables the same interface to be used for different underlying forms
(data types).

Real-World Example - The Car:

 The design of a car is a classic example of OOP principles in action:


o Inheritance: Different types of vehicles (like a sedan or truck) inherit common
properties (like wheels and engine) from a general Vehicle class.
o Encapsulation: The driver interacts with the car through simple controls like the steering
wheel and pedals, which encapsulate the complex mechanisms of the vehicle.
o Polymorphism: The car may have different driving modes (e.g., sports mode, eco
mode), but the driver uses the same controls regardless of the mode.

1.2 History of Java:

 Java, introduced in 1995 by Sun Microsystems, quickly became a revolutionary programming


language. Unlike many software systems that evolve slowly, Java has undergone rapid and
significant development since its inception.

Java 1.0 and Java 1.1:

 Java 1.0: The initial release of Java was groundbreaking, introducing the concept of platform-
independent, "write once, run anywhere" programming.
 Java 1.1: Soon after Java 1.0, Java 1.1 was released. This version introduced substantial
changes, including new library elements, a redefined event handling model, and reconfiguration
of existing features. Java 1.1 also deprecated some of the features from Java 1.0, signaling Java's
continuous evolution.

Java 2 (Java 1.2):

 Java 2 (Version 1.2): The next major release, Java 2, marked the beginning of Java's "modern
age." Java 2 introduced significant enhancements, including the Swing GUI toolkit, the
Collections Framework, and improved the Java Virtual Machine (JVM). Java 2 was repackaged
as J2SE (Java 2 Platform, Standard Edition).

Java 1.3 and 1.4:

 Java 1.3: This version focused on improving and refining the existing features introduced in
Java 2, maintaining source-code compatibility with version 1.2.
 Java 1.4: Introduced important features like the assert keyword, chained exceptions, and a
channel-based I/O subsystem. This version also saw enhancements to the Collections
Framework and networking classes.

J2SE 5 (Java 1.5):

 Java 5 (Version 1.5): A revolutionary release that fundamentally expanded the Java language.
Major new features included generics, annotations, autoboxing and auto-unboxing,
enumerations, the enhanced for-each loop, variable-length arguments (varargs), static import,
formatted I/O, and concurrency utilities. The significance of these features led to the version
being labeled as Java 5 instead of Java 1.5.
Java SE 6:

 Java SE 6 (Version 1.6): This version continued to build on the advancements of Java 5, adding
incremental improvements to the API libraries, new packages, and runtime enhancements.
Although no major language features were added, Java SE 6 solidified the changes from Java 5.

Java SE 7:

 Java SE 7 (Version 1.7): The first major release after Oracle acquired Sun Microsystems, Java
SE 7 introduced several new features as part of Project Coin, including:
o String in switch statements
o Binary integer literals and underscores in numeric literals
o The try-with-resources statement for automatic resource management
o Type inference with the diamond operator
o Enhanced exception handling with multi-catch and improved type checking
o Fork/Join Framework for parallel programming
o Enhancements to the NIO framework (NIO.2)

Java SE 7 was significant for its improvements to the language and API, especially in simplifying
coding practices and supporting modern multicore processors.

1.3 Java buzzwords

Java, as a programming language, was designed with a specific set of principles in mind that guided its
development. These principles are often referred to as the "Java Buzzwords," and they encapsulate the
core values and features that the language embodies. Below is an exploration of these buzzwords:

1. Simple:

 Java was created to be easy to learn, especially for developers familiar with C and C++. By
adopting much of the syntax from C/C++, Java makes the transition to this new language
smooth and intuitive. Java also eliminated the complexities of languages like C++, such as
multiple inheritance and operator overloading, making it simpler and less error-prone.

2. Secure:
 Security was a key consideration in the design of Java. It was initially designed for use in
networked environments, which required strong security measures. Java programs run inside a
secure "sandbox" environment provided by the Java Virtual Machine (JVM), which prevents
untrusted code from accessing unauthorized resources.

3. Portable:

 Java's portability comes from its "write once, run anywhere" philosophy. Java code is compiled
into an intermediate form called bytecode, which can be executed on any machine that has a
JVM. This makes Java programs highly portable across different platforms, including different
operating systems and hardware architectures.

4. Object-Oriented:

 Java is fundamentally an object-oriented programming (OOP) language. This means that the
language is designed around the concept of objects, which combine data and methods. Java's
OOP principles include inheritance, encapsulation, polymorphism, and abstraction, which help
in creating modular, reusable, and scalable software.

5. Robust:

 Java was designed with a focus on reliability and error-free code. It emphasizes early error
checking, such as type checking at compile-time and runtime, and it includes features like
garbage collection to manage memory automatically. These features reduce the likelihood of
bugs related to memory management, which are common in languages like C/C++.

6. Multithreaded:

 Java provides built-in support for multithreading, which allows the simultaneous execution of
multiple parts of a program. This is essential for developing interactive applications, such as
games or GUIs, and for efficiently utilizing modern multicore processors. Java’s thread
management is designed to be easy to implement, helping developers write responsive, high-
performance programs.

7. Architecture-Neutral:

 Java’s architecture-neutral nature means that its programs are not tied to any specific processor
or operating system. This is achieved through the use of bytecode, which can be interpreted by
any JVM, regardless of the underlying architecture. This ensures that Java programs are future-
proof, as they are insulated from changes in hardware and system software.

8. Interpreted and High Performance:

 Java is an interpreted language, where the compiled bytecode is executed by the JVM. Despite
being interpreted, Java achieves high performance through Just-In-Time (JIT) compilation,
which translates bytecode into native machine code at runtime, allowing Java applications to run
almost as fast as native compiled languages.

9. Distributed:

 Java is inherently designed for networked environments, supporting distributed computing


through features like Remote Method Invocation (RMI) and support for Internet protocols
(TCP/IP). These features allow Java programs to easily interact with other resources across a
network, making it ideal for building distributed applications.

10. Dynamic:

 Java is a dynamic language in that it supports dynamic memory allocation and can dynamically
link new classes, methods, or objects during runtime. Java programs carry significant runtime
type information that is used to verify and resolve accesses to objects at runtime, ensuring safe
and efficient code execution

11. Java is platform-independent :

 It is because Java programs are compiled into bytecode, which can be executed on any device
or operating system with a compatible Java Virtual Machine (JVM). This means you can write
your code once and run it anywhere, adhering to the principle "Write Once, Run Anywhere"
(WORA). The JVM interprets the bytecode into native machine code specific to the platform,
ensuring consistency and portability across different systems.

1.4 JVM Architecture


1. Overview: The Java Virtual Machine (JVM) is a crucial component of the Java runtime environment
that enables Java applications to run on various hardware and software platforms without modification.
The JVM abstracts the underlying operating system and hardware, providing a consistent execution
environment for Java bytecode.

2. Key Components:

 Class Loader Subsystem:


o Function: Responsible for loading, linking, and initializing classes and interfaces.
o Class Loading Process:
 Loading: The class loader reads the class file from the file system or network and
loads it into memory.
 Linking: Verifies the bytecode, prepares the class by resolving symbolic
references, and initializes static variables.
 Initialization: Executes static initialization blocks and static variable
initializations.
 Runtime Data Areas:
o Method Area: Stores class-level data, including runtime constant pool, field and method
data, and method and constructor code.
o Heap: The runtime data area where all class instances and arrays are allocated. It is
shared among all threads.
o Stack: Each thread has its own stack, which stores frames. Each frame contains local
variables, operand stacks, and dynamic linking information.
o PC Register: Each thread has a Program Counter (PC) register that holds the address of
the currently executing instruction.
o Native Method Stack: Supports native methods, i.e., methods written in languages other
than Java, like C or C++.
Java Development Environment

• Edit
– Create/edit the source code
• Compile
– Compile the source code
• Load
– Load the compiled code
• Verify
– Check against security restrictions
• Execute
– Execute the compiled code

 Execution Engine:
o Interpreter: Reads and executes bytecode instructions one at a time. It is
straightforward but can be slower.
o Just-In-Time (JIT) Compiler: Converts bytecode into native machine code just before
execution. This improves performance by reducing interpretation overhead.
o Garbage Collector (GC): Automatically manages memory by reclaiming unused
objects and preventing memory leaks. Various GC algorithms are employed, including
generational, mark-and-sweep, and more.
 Native Interface:
o Java Native Interface (JNI): Allows Java code to interact with native applications and
libraries written in other languages like C or C++.
o Java Native Access (JNA): Provides a simpler way to interact with native libraries
without writing JNI code.
 Native Method Libraries:
o These are libraries that provide the native implementations required for JVM operations,
such as file system operations or network communications.

3. Execution Flow:

1. Loading: The JVM class loader loads the required classes.


2. Verification: The bytecode verifier checks the loaded classes for security and correctness.
3. Execution: The execution engine interprets or compiles the bytecode into native machine code.
The JIT compiler may optimize the code to improve performance.
4. Memory Management: The garbage collector reclaims memory from objects that are no longer
in use.

4. JVM Specifications:

 Java Virtual Machine Specification: Defines the JVM's architecture and how it should behave.
 Java Class File Format Specification: Describes the format of Java class files, including
bytecode and metadata.

5. JVM Implementations: Different JVM implementations may have variations in how they handle
these components, but they all adhere to the JVM specification to ensure compatibility.

1.5 Data types- Variables, Scope and life time of variables,

Primitive Data Types

Java's primitive data types are the simplest forms of data representation. They are not objects and are
used to represent single values. They are categorized into four groups:

1. Integers:
o byte: 8-bit signed integer. Range: -128 to 127.
o short: 16-bit signed integer. Range: -32,768 to 32,767.
o int: 32-bit signed integer. Range: -2,147,483,648 to 2,147,483,647.
o long: 64-bit signed integer. Range: -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807.
2. Floating-Point Numbers:
o float: 32-bit single-precision floating-point number. Range: 1.4e–045 to 3.4e+038.
o double: 64-bit double-precision floating-point number. Range: 4.9e–324 to 1.8e+308.
3. Characters:
o char: 16-bit Unicode character. Range: 0 to 65,535.
4. Boolean:
o boolean: Represents true or false values.

Variables

A variable in Java is a container for storing data values. Each variable must be declared with a type
before it can be used

Declaration Syntax:

type identifier [ = value ][, identifier [= value ] …];

Dynamic Initialization

Variables can be initialized dynamically using expressions:

double a = 3.0, b = 4.0;

double c = Math.sqrt(a * a + b * b);

System.out.println("Hypotenuse is " + c);


Scope and Lifetime of Variables

 Scope: Determines where the variable can be accessed. Variables have scope based on where
they are declared.
 Lifetime: The duration a variable exists in memory.
 Method Scope: Variables declared within a method are only accessible within that method.
 Block Scope: Variables declared within a block (e.g., inside {}) are only accessible within that
block.

// Demonstrates various data types, variables, and scope

class DataTypesDemo {

public static void main(String[] args) {

// Integer types

byte b = 10;

short s = 1000;

int i = 200000;

long l = 3000000000L;

// Floating-point types

float f = 3.14f;

double d = 3.14159;

// Character type

char c = 'A';

// Boolean type

boolean flag = true;

// Dynamic Initialization

double radius = 7.5;

double area = Math.PI * radius * radius;

// Variable Scope

int outerVar = 10; // Accessible throughout the method

System.out.println("Outer Variable: " + outerVar);

// Start of block scope

{
int innerVar = 20; // Accessible only within this block

System.out.println("Inner Variable: " + innerVar);

// Access outerVar inside block

System.out.println("Access outerVar inside block: " + outerVar);

// Trying to access innerVar here would cause a compile-time error

// System.out.println(innerVar); // Uncommenting this line will cause an error

System.out.println("byte: " + b);

System.out.println("short: " + s);

System.out.println("int: " + i);

System.out.println("long: " + l);

System.out.println("float: " + f);

System.out.println("double: " + d);

System.out.println("char: " + c);

System.out.println("boolean: " + flag);

System.out.println("Area of circle with radius " + radius + " is: " + area);

OUTPUT:

Outer Variable: 10

Inner Variable: 20

Access outerVar inside block: 10

byte: 10

short: 1000

int: 200000

long: 3000000000

float: 3.14

double: 3.14159
char: A

boolean: true

Area of circle with radius 7.5 is: 176.71458676442586

1.6 Arrays

An array is a collection of variables of the same type that are stored in contiguous memory locations.
Arrays can be of any type, including primitive types (e.g., int, float, char, boolean) or reference types
(e.g., String, user-defined classes). One-Dimensional Arrays
One-Dimensional Arrays
Declaration
type[] arrayName;
 type: The type of elements in the array.
 arrayName: The name of the array variable.
Example: int month_days[];
Allocation
To allocate memory for the array, use the new keyword:
arrayName = new type[size];

 type: The data type of array elements. size: The number of elements in the array.

Example: month_days = new int[12];


Initialization

Arrays can be initialized with specific values during declaration:


int[] month_days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

Accessing Elements

Elements in the array are accessed using their index:


month_days[1] = 28; // Assigns 28 to the second element
System.out.println(month_days[3]); // Prints the fourth element

Multidimensional Arrays

Multidimensional arrays are arrays of arrays. They can have two or more dimensions.
Declaration

To declare a two-dimensional array:


type[][] arrayName;

Example: int[][] twoD;


Allocation

To allocate memory for a multidimensional array, specify the size of each dimension:
arrayName = new type[rows][columns];

Example: twoD = new int[4][5];


Initialization

Multidimensional arrays can be initialized with specific values:


int[][] twoD = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

Accessing Elements

Access elements using indexes for each dimension:


twoD[1][2] = 6; // Accesses the element at row 1, column 2

A conceptual view of a 4 by 5, two-dimensional array

Example Program - Two-Dimensional Array:


class TwoDArray {
public static void main(String args[]) {
int twoD[][] = new int[4][5];
int i, j, k = 0;
for (i = 0; i < 4; i++)
for (j = 0; j < 5; j++) {
twoD[i][j] = k;
k++;
}
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}
JAGGED ARRAYS
Jagged arrays are arrays with different lengths for each sub-array. They are created by allocating
memory for each sub-array separately.

T-dimensional array - sizes of the second dimension are unequal


Example Program - Jagged Array:
class TwoDAgain {
public static void main(String args[]) {
int[][] twoD = new int[4][];
twoD[0] = new int[1];
twoD[1] = new int[2];
twoD[2] = new int[3];
twoD[3] = new int[4];
int i, j, k = 0;
for (i = 0; i < 4; i++)
for (j = 0; j < i + 1; j++) {
twoD[i][j] = k;
k++;
}
for (i = 0; i < 4; i++) {
for (j = 0; j < i + 1; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}
1.7 Operators
Operators are special symbols in Java that perform operations on variables and values. Java operators
can be categorized into several types:
1. Arithmetic Operators
 Purpose: Perform basic arithmetic operations.
 Operators: +, -, *, /, %
o + : Addition
o - : Subtraction
o * : Multiplication
o / : Division
o % : Modulus (remainder of division)
2. Relational Operators
 Purpose: Compare two values and determine the relationship.
 Operators: ==, !=, >, <, >=, <=
o == : Equal to
o != : Not equal to
o > : Greater than
o < : Less than
o >= : Greater than or equal to
o <= : Less than or equal to
3. Logical Operators
 Purpose: Combine or invert boolean values.
 Operators: &&, ||, !
o && : Logical AND (true if both operands are true)
o || : Logical OR (true if at least one operand is true)
o ! : Logical NOT (inverts the boolean value)

4. Assignment Operators
 Purpose: Assign values to variables.
 Operators: =, +=, -=, *=, /=, %=
o = : Simple assignment
o += : Add and assign
o -= : Subtract and assign
o *= : Multiply and assign
o /= : Divide and assign
o %= : Modulus and assign
5. Increment and Decrement Operators
 Purpose: Increase or decrease a variable's value by 1.
 Operators: ++, --
o ++ : Increment (prefix or postfix)
o -- : Decrement (prefix or postfix)

6. Bitwise Operators
 Purpose: Perform bit-level operations.
 Operators: &, |, ^, ~, <<, >>, >>>
o & : Bitwise AND
o | : Bitwise OR
o ^ : Bitwise XOR
o ~ : Bitwise NOT
o << : Left shift
o >> : Right shift
o >>>: Unsigned right shift

7. Conditional (Ternary) Operator


 Purpose: Evaluate a condition and choose between two values.
 Operator: ? :
o condition ? trueExpression : falseExpression

8. Instanceof Operator
 Purpose: Test whether an object is an instance of a specific class or subclass.
 Operator: instanceof

9. Type Cast Operator


 Purpose: Convert a variable from one type to another.
 Syntax: (type) value

Example:
public class SimpleOperatorDemo {
public static void main(String[] args) {
// Arithmetic Operators
int a = 15, b = 4;
System.out.println("a + b = " + (a + b)); // Addition
System.out.println("a - b = " + (a - b)); // Subtraction
System.out.println("a * b = " + (a * b)); // Multiplication
System.out.println("a / b = " + (a / b)); // Division
System.out.println("a % b = " + (a % b)); // Modulus
// Relational Operators
System.out.println("a == b: " + (a == b)); // Equal to
System.out.println("a != b: " + (a != b)); // Not equal to
System.out.println("a > b: " + (a > b)); // Greater than
System.out.println("a < b: " + (a < b)); // Less than
// Logical Operators
boolean x = true, y = false;
System.out.println("x && y: " + (x && y)); // AND
System.out.println("x || y: " + (x || y)); // OR
System.out.println("!x: " + (!x)); // NOT
// Assignment Operator
int c = 10;
c += 5; // Equivalent to c = c + 5
System.out.println("c += 5: " + c); // 15
// Increment Operator
int d = 5;
System.out.println("d++: " + d++); // 5 (then d becomes 6)
System.out.println("++d: " + ++d); // 7
}
}
1.8 control statements
1. Control Statements

a. Conditional Statements

 if Statement: Executes a block of code if its condition evaluates to true.


 if-else Statement: Executes one block of code if the condition is true, and another block if it's false.
 else-if Statement: Allows multiple conditions to be checked in sequence.
 switch Statement: Selects one of many code blocks to execute based on a variable's value.
b. Jump Statements

break: Exits from the current loop or switch statement.

continue: Skips the current iteration of a loop and proceeds with the next iteration.

return: Exits from the current method and optionally returns a value.
2. Loops
a. for Loop
 Executes a block of code a specific number of times.
 Syntax:
for (initialization; condition; update) {
// code to be executed
}
b. while Loop
 Executes a block of code as long as the condition is true.
 Syntax:
while (condition) {
// code to be executed
}
c. do-while Loop
 Executes a block of code at least once, then continues executing it as long as the condition is true.
 Syntax:
do {
// code to be executed
} while (condition);
Program
public class ControlStatementsAndLoops {
public static void main(String[] args) {
int number = 10;
// Conditional Statements
if (number > 0) {
System.out.println("The number is positive.");
} else if (number < 0) {
System.out.println("The number is negative.");
} else {
System.out.println("The number is zero.");
}
// Switch Statement
String day = "Monday";
switch (day) {
case "Monday":
System.out.println("Start of the work week.");
break;
case "Wednesday":
System.out.println("Midweek.");
break;
case "Friday":
System.out.println("End of the work week.");
break;
default:
System.out.println("It's the weekend!");
break;
}
// For Loop
System.out.println("For loop output:");
for (int i = 1; i <= 5; i++) {
System.out.println("i = " + i);
}
// While Loop
System.out.println("While loop output:");
int j = 1;
while (j <= 5) {
System.out.println("j = " + j);
j++;
}
// Do-While Loop
System.out.println("Do-while loop output:");
int k = 1;
do {
System.out.println("k = " + k);
k++;
} while (k <= 5);
// Break and Continue
System.out.println("Break and Continue example:");
for (int m = 1; m <= 10; m++) {
if (m == 5) {
System.out.println("Breaking loop at m = " + m);
break; // Exit the loop
}
if (m % 2 == 0) {
continue; // Skip even numbers
}
System.out.println("Odd number m = " + m);
}
// Return statement
System.out.println("Return statement example:");
int result = sum(10, 20);
System.out.println("Sum of 10 and 20 is " + result);
}
// Method with return statement
public static int sum(int a, int b) {
return a + b;
}
}
OUTPUT
The number is positive.
Start of the work week.
For loop output:
i=1
i=2
i=3
i=4
i=5
While loop output:
j=1
j=2
j=3
j=4
j=5
Do-while loop output:
k=1
k=2
k=3
k=4
k=5
Break and Continue example:
Odd number m = 1
Odd number m = 3
Odd number m = 5
Breaking loop at m = 5
Return statement example:
Sum of 10 and 20 is 30
1.8 Type conversion and casting:
Type Conversion and Casting in Java

Type conversion and casting are fundamental concepts in Java that enable the assignment of values
between variables of different types. Here's an overview of automatic type conversions, explicit casting,
and type promotion rules:

1. Automatic Type Conversions


Java performs automatic type conversion when two types are compatible, and the destination type is
larger than the source type. This process is known as widening conversion.

 Compatible Types: Numeric types (e.g., int, long, float) can be automatically converted if the
destination type can accommodate the value of the source type.
 No Automatic Conversion: There is no automatic conversion between numeric types and char or
boolean. For example, you cannot directly assign an int to a char or boolean variable without a cast.

int i = 10;
long l = i; // int to long (widening conversion)
float f = 3.14f;
double d = f; // float to double (widening conversion)
2. Casting Incompatible Types
When automatic conversion is not possible, explicit casting is required. Casting is used to convert a
value from one type to another, especially when the destination type is smaller than the source type
(narrowing conversion).

 Cast Syntax: (target-type) value


 Narrowing Conversion: When converting from a larger type to a smaller type, such as from int to byte.
int i = 257;
byte b = (byte) i; // int to byte (narrowing conversion)
System.out.println(b); // Output: 1
Special Cases:

 Floating-Point to Integer: Converting from float or double to an integer type truncates the
fractional part.
 Overflow: If the value exceeds the range of the target type, it wraps around according to the
type's range.

3. Automatic Type Promotion in Expressions


Java promotes operands in expressions to handle different precision levels. This is known as automatic
type promotion.

 Promotion Rules:
1. byte, short, char → Promoted to int.
2. int → Promoted to long if combined with a long.
3. long → Promoted to float if combined with a float.
4. float → Promoted to double if combined with a double.

byte a = 40;
byte b = 50;
byte c = 100;
int d = a * b / c; // byte to int promotion in expression
System.out.println(d); // Output: 20
Compilation Error Example:
byte b = 50;
b = b * 2; // Error! b * 2 results in int, cannot assign to byte
b = (byte)(b * 2); // Correct, explicit cast to byte
1.9 Simple java program
A class is declared by use of the class keyword. The classes that have been used up to this point
are actually very limited examples of its complete form. Classes can (and usually do) get much more
complex A simplified general form of a class definition is shown here:
class classname
{
type instance-variable1; // ...
type instance-variableN;
type methodname1(parameter-list)
{
// body of method
}
type methodname2(parameter-list)
{
// body of method
}
// ...
type methodnameN(parameter-list)
{
// body of method
}
}
1.10 Constructors- Methods

1. Introduction to Constructors:

Definition: A constructor is a special method in a class that is automatically called when an


object is created. Its primary role is to initialize the object.
 Characteristics:
o Same name as the class.
o No return type (not even void).
o Automatically invoked during object creation.
o Can have multiple constructors (overloading) with different parameter lists.
2. Default Constructor:
 Definition: If no constructor is explicitly defined, Java provides a default constructor that
initializes all instance variables to their default values (e.g., 0 for numeric types, null for object
references).
 Example:
class Box {
double width;
double height;
double depth;
// Default constructor provided by Java
}
3. Parameterized Constructors:
 Definition: Constructors that accept parameters allow for more flexible and specific
initialization of objects.
 Usage:
o Initialize instance variables with values provided during object creation.
 Example:
class Box {
double width;
double height;
double depth;

// Parameterized constructor
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
}
 Usage in Main Method:
public class BoxDemo {
public static void main(String[] args) {
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box(3, 6, 9);
System.out.println("Volume of mybox1: " + mybox1.volume());
System.out.println("Volume of mybox2: " + mybox2.volume());
}
}
4. The this Keyword:
 Definition: this is a reference variable that refers to the current object within an instance
method or constructor.
 Usage:
o Resolves naming conflicts between instance variables and method parameters.
o Accesses instance variables and methods from within the class.
 Example:
class Box {
double width;
double height;
double depth;

// Constructor with 'this' keyword


Box(double width, double height, double depth) {
this.width = width;
this.height = height;
this.depth = depth;
}
}
5. Constructor Overloading:
 Definition: A class can have multiple constructors with different parameter lists. This is known
as constructor overloading.
 Example:
class Box {
double width;
double height;
double depth;

// Default constructor
Box() {
width = 1;
height = 1;
depth = 1;
}

// Parameterized constructor
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
}
6. Garbage Collection:
 Definition: Automatic memory management process that reclaims memory used by objects that
are no longer referenced.
 Key Points:
o Java handles garbage collection automatically, reducing the need for manual memory
management.
o Garbage collection occurs sporadically and is not directly controlled by the programmer.
7. The finalize() Method:
 Definition: A method that can be overridden to define actions that should be performed before
an object is reclaimed by the garbage collector.
 Usage:
o Typically used to release non-Java resources (e.g., file handles, network connections).
 Syntax:
protected void finalize() {
// Cleanup code before garbage collection
}
1.11 Static block, Static Data, Static Method
1. Static Data (Static Variables):
 Definition: Static variables, also known as class variables, are shared among all instances of a
class. They belong to the class rather than to any specific instance.
 Characteristics:
o Declared with the static keyword.
o Initialized once, at the start of the execution.
o Accessible via the class name as well as through instances of the class.
 Usage:
o Used to store data common to all objects of the class.
 Example:
class Counter {
static int count = 0; // static variable

Counter() {
count++; // Increment count whenever a new object is created
}
}

public class TestStatic {


public static void main(String[] args) {
new Counter();
new Counter();
System.out.println("Count: " + Counter.count); // Output: Count: 2
}
}
2. Static Methods:
 Definition: Static methods belong to the class rather than any object instance. They can be
called without creating an instance of the class.
 Characteristics:
o Declared with the static keyword.
o Can only access static data and call other static methods.
o Cannot access instance variables or methods directly.
 Usage:
o Used for utility or helper methods that do not depend on object state.
 Example:
class MathUtility {
static int square(int x) {
return x * x;
}
}

public class TestStaticMethod {


public static void main(String[] args) {
int result = MathUtility.square(5); // Call static method without
creating an instance
System.out.println("Square: " + result); // Output: Square: 25
}
}
3. Static Blocks:
 Definition: Static blocks are used for static initialization of a class. They run once when the
class is loaded into memory.
 Characteristics:
o Defined using the static keyword followed by a block of code.
o Useful for initializing static variables or performing setup that is required before any
static method or variable is accessed.
 Usage:
o Typically used to initialize complex static variables or perform tasks that must be done
only once.
 Example:
class Initialization {
static int value;

static {
value = 10; // Initialize static variable
System.out.println("Static block executed");
}
}

public class TestStaticBlock {


public static void main(String[] args) {
System.out.println("Value: " + Initialization.value); // Output:
Static block executed Value: 10
}
}
1.12 String and String Buffer Classes

1. String Class:

 Definition: The String class represents a sequence of characters and is one of the most
commonly used classes in Java. Strings are immutable, meaning once a String object is created,
it cannot be modified.
 Characteristics:
o Immutable: Any modification creates a new String object.
o Defined in: java.lang package.
o Syntax: String str = "Hello";
 Key Methods:
o length(): Returns the length of the string.

String s = "Hello";
int len = s.length(); // len = 5

o charAt(int index): Returns the character at the specified index.

char ch = s.charAt(1); // ch = 'e'

o substring(int beginIndex, int endIndex): Returns a new string that is a substring


of the original string.

String sub = s.substring(1, 4); // sub = "ell"

o indexOf(String str): Returns the index of the first occurrence of the specified
substring.

int index = s.indexOf("ll"); // index = 2

o replace(char oldChar, char newChar): Returns a new string where all occurrences
of the specified character are replaced.

String replaced = s.replace('l', 'p'); // replaced = "Heppo"

o trim(): Removes leading and trailing whitespace.

String trimmed = " Hello ".trim(); // trimmed = "Hello"

o equals(Object obj): Compares the string with the specified object for equality.

boolean isEqual = s.equals("Hello"); // isEqual = true

o toLowerCase(): Converts all characters in the string to lowercase.


String lower = s.toLowerCase(); // lower = "hello"

o toUpperCase(): Converts all characters in the string to uppercase.

String upper = s.toUpperCase(); // upper = "HELLO"

Example:

public class StringExample {


public static void main(String[] args) {
String str = "Java Programming";
System.out.println("Length: " + str.length());
System.out.println("Character at index 5: " + str.charAt(5));
System.out.println("Substring from index 5: " + str.substring(5));
System.out.println("Index of 'Programming': " +
str.indexOf("Programming"));
System.out.println("Replace 'Java' with 'Python': " +
str.replace("Java", "Python"));
System.out.println("Trimmed String: '" + " Hello World ".trim() +
"'");
}
}

2. StringBuffer Class:

 Definition: The StringBuffer class represents a mutable sequence of characters. Unlike


String, StringBuffer objects can be modified after they are created.
 Characteristics:
o Mutable: Allows modifications to the character sequence.
o Defined in: java.lang package.
o Syntax: StringBuffer sb = new StringBuffer("Hello");
 Key Methods:
o append(String str): Appends the specified string to this StringBuffer.

StringBuffer sb = new StringBuffer("Hello");


sb.append(" World"); // sb = "Hello World"
o insert(int offset, String str): Inserts the specified string at the specified
position.

sb.insert(5, " Java"); // sb = "Hello Java World"


o delete(int start, int end): Removes the substring from the specified start index to
end index.

sb.delete(5, 10); // sb = "Hello World"


o reverse(): Reverses the sequence of characters in the StringBuffer.

sb.reverse(); // sb = "dlroW olleH"


o replace(int start, int end, String str): Replaces the substring from start to
end index with the specified string.

sb.replace(5, 11, "Java"); // sb = "Hello Java"

o toString(): Converts the StringBuffer to a String.

String result = sb.toString(); // result = "Hello Java"


 Example:
public class StringBufferExample {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("Java");
sb.append(" Programming");
System.out.println("After append: " + sb);

sb.insert(4, " Language");


System.out.println("After insert: " + sb);

sb.delete(4, 14);
System.out.println("After delete: " + sb);

sb.reverse();
System.out.println("After reverse: " + sb);

sb.replace(0, 5, "Python");
System.out.println("After replace: " + sb);

String str = sb.toString();


System.out.println("Converted to String: " + str);
}
}

Aspect String StringBuffer


Immutable: once created, the value
Mutability Mutable: can be modified after creation.
cannot be changed.
Less efficient for frequent
More efficient for modifications; changes are
Performance modifications; new objects are created
made in the same object.
for each change.
May consume more memory due to
Generally more memory efficient as it
Memory Usage creation of new objects for each
modifies the existing object.
change.
Not thread-safe by default. For thread safety,
Immutable nature provides inherent
Thread Safety use StringBuffer with synchronization or
thread safety.
StringBuilder.
length(), charAt(), substring(),
Common indexOf(), replace(), append(), insert(), delete(), reverse(),
Methods toUpperCase(), toLowerCase(), replace(), toString()
trim()
Typically initialized directly with a Initialized through constructors and modified
Initialization
literal or through concatenation. using methods.
Ideal for strings that need frequent
Usage Ideal for fixed and constant strings.
modifications.
Conversion to Automatically done when using Requires explicit call to toString() method
String toString() method. for conversion.
StringBuffer sb = new
Example String str = "Hello";
StringBuffer("Hello");
Example of
str = str.concat(" World"); sb.append(" World");
Modification

You might also like