0% found this document useful (0 votes)
3 views2 pages

Immutable Objects, Variable Scope, and Recursion 1. Immutable Objects and Their Effect On Passing by Reference

The document discusses three key concepts in Java: immutable objects, variable scope, and recursion. Immutable objects, such as Strings, cannot be modified after creation, which helps prevent side effects in concurrent applications. Variable scope defines the visibility of variables, while recursion is a technique where a method calls itself to solve problems, requiring careful implementation to avoid issues like stack overflow.

Uploaded by

ka0221660
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Immutable Objects, Variable Scope, and Recursion 1. Immutable Objects and Their Effect On Passing by Reference

The document discusses three key concepts in Java: immutable objects, variable scope, and recursion. Immutable objects, such as Strings, cannot be modified after creation, which helps prevent side effects in concurrent applications. Variable scope defines the visibility of variables, while recursion is a technique where a method calls itself to solve problems, requiring careful implementation to avoid issues like stack overflow.

Uploaded by

ka0221660
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Prepared by : Muhammad Shwan

Immutable Objects, Variable Scope, and Recursion

1. Immutable Objects and Their Effect on Passing by Reference


In Java, all objects are passed by value, but the value passed is the reference to the
object, not the object itself. However, immutability plays a crucial role in how this
reference behaves. An immutable object is an object whose state cannot be modified
after creation. The most common example is the String class.

When an immutable object is passed to a method, changes made to the reference


inside the method do not affect the original object. This is because any 'modification'
to an immutable object results in the creation of a new instance, leaving the original
untouched.

Example:
public void changeString(String str) {
str = "New Value";
}
Calling changeString(myStr); will not change the value of myStr outside the method.
This gives immutability a strong role in maintaining predictable, side-effect-free
code, especially in concurrent applications where multiple threads may access the
same object.

In contrast, mutable objects (like arrays or user-defined classes) allow their fields to
be changed even when passed by reference. Developers must be careful with
mutable objects to avoid unintended side effects.

2. Variable Scope in Java


Scope defines the visibility and lifetime of variables within a program. Java primarily
uses block scope, which means a variable is only accessible within the block of code
where it is declared.

There are several levels of scope in Java:


- Class Scope (Fields): Variables declared directly inside a class but outside methods
are accessible by all methods within that class.
- Method Scope (Local Variables): Variables declared inside a method are only
available within that method.

1
- Block Scope: Variables declared inside loops, if statements, or other code blocks
are only accessible within those blocks.

Example:
public class Example {
int classLevel = 10; // Class scope

public void demoMethod() {


int methodLevel = 5; // Method scope
if (methodLevel > 0) {
int blockLevel = 3; // Block scope
}
// blockLevel is not accessible here
}
}
Understanding scope helps prevent naming conflicts, memory issues, and
unexpected behavior due to variable shadowing.

3. Recursion
Recursion is a programming technique where a method calls itself to solve a
problem. Each recursive call breaks the problem into smaller subproblems until a
base case is reached, which halts further recursive calls.

A classic example is calculating factorial:


public int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}
Here, the function keeps calling itself with n-1 until n equals 0.

While recursion simplifies the logic for problems like tree traversal, Fibonacci
series, and backtracking algorithms, it can lead to stack overflow if not implemented
carefully. It's essential to define a base case and ensure it will eventually be met.

Moreover, recursive solutions may sometimes be inefficient due to repeated


calculations. In such cases, memoization or converting to iteration is preferred for
performance.

You might also like