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

Objects Recr

The document discusses various Java programming concepts including returning objects from methods, recursion, and the finalize method for resource cleanup. It explains how to calculate sums and factorials using both iterative and recursive approaches, and highlights the importance of garbage collection for memory management in Java applications. Additionally, it outlines the role of the finalize method in releasing resources before objects are garbage collected.

Uploaded by

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

Objects Recr

The document discusses various Java programming concepts including returning objects from methods, recursion, and the finalize method for resource cleanup. It explains how to calculate sums and factorials using both iterative and recursive approaches, and highlights the importance of garbage collection for memory management in Java applications. Additionally, it outlines the role of the finalize method in releasing resources before objects are garbage collected.

Uploaded by

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

Returning objects

Methods in Java can return objects, allowing for flexible and reusable code
public class Test {

static int[] getSumAndSub(int a, int b)


{
int[] ans = new int[2];
ans[0] = a + b;
ans[1] = a - b;

// returning array of elements


return ans;
}

public static void main(String[] args)


{
int[] ans = getSumAndSub(100, 50);
System.out.println("Sum = " + ans[0]);
System.out.println("Sub = " + ans[1]);
}
}
--------------------
Recursion.
It is the process of defining something in terms of itself.

public class FactorialExample{


public static void main(String args[]){
int i,fact=1;
int number=5;//It is the number to calculate factorial
for(i=1;i<=number;i++){
fact=fact*i;
}
System.out.println("Factorial of "+number+" is: "+fact);
}

using recursion

public class Main{


int fact(int n)
{
int result;
if(n==1) return 1;
result = fact(n-1)*n;
return result;
}

public static void main(String p[])


{
Main f = new Main();
System.out.println("factorial of number is:"+f.fact(5));
}
}
------------------
To add numbers example

public class Main {


public static void main(String[] args) {
int result = sum(10);
System.out.println(result);
}
public static int sum(int k) {
if (k > 0) {
return k + sum(k - 1);
} else {
return 0;
}
}
}

o/p
10 + sum(9)
10 + ( 9 + sum(8) )
10 + ( 9 + ( 8 + sum(7) ) )
...
10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + sum(0)
10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + 0

-------------------------------
The finalize() method in Java is a mechanism provided by the Object class that
allows developers to perform cleanup operations on objects before they are
garbage collected.

The primary purpose of finalize() is to allow an object to release resources


such as file handles, network connections, or database connections
before being garbage collected.
This is particularly useful for managing system resources that are not
automatically
handled by Java's memory management

Syntax
Protected void finalize()
{
Clean up code
}
---------
Garbage collection in Java is an automatic memory management process that helps
reclaim memory used by objects that are no longer needed. This is crucial for
preventing memory leaks and ensuring efficient use of resources within Java
applications.
What is Garbage Collection?
Definition: Garbage collection (GC) is the process by which the Java Virtual
Machine (JVM) automatically identifies and removes objects that are no longer
referenced by the program, thus freeing up memory space124.
Memory Management: In Java, objects are created in a memory area known as the heap.
When these objects are no longer in use, the garbage collector reclaims their
memory,
preventing potential OutOfMemoryError situations that can occur if the heap
runs out of space.
-------------

You might also like