Recursive Functions
“a function that calls itself” (i.e) a function execution instance calls another execution instance of the same function
In recursive problem solving, a problem is repeatedly broken down into similar subproblems, until the subproblems can be
directly solved without further breakdown.
Recursive Formula : Decompose the original problem into simpler instances of the same problem
Find Base Case : As the large problem is broken down into successively less complex ones, those subproblems must eventually
become so simple that they can be solved without further subdivision.
Fibonacci Number Computation
L.Sumathi AP/CSE ,GCT,CBE
Recursion
// A simple example of recursion.
class Factorial {
int fact(int n) {
int result;
if(n==1) return 1;
result = fact(n-1) * n;
return result;
}
}
public class Recursion {
public static void main(String args[]) {
Factorial f = new Factorial();
System.out.println("Factorial of 4 is " +
f.fact(4));
}
}
http://www.pythontutor.com/
L.Sumathi ,AP/CSE ,GCT 2
Programming in Java : Unit I L.Sumathi
3
,AP/CSE ,GCT
Trace recursion
9
The fib() function is invoked for ____________ times
Try it in Java L.Sumathi AP/CSE ,GCT,CBE
Variable-Length Arguments
Method overload - maximum number of arguments was small and known
Array - maximum number of potential arguments was larger, or unknowable
public static void main(String args[])
class PassArray { {
static void vaTest(int v[]) { // Notice how an array must be created to
System.out.print("Number of args: " + v.length // hold the arguments.
+" Contents: "); int n1[] = { 10 };
for(int x : v) int n2[] = { 1, 2, 3 };
System.out.print(x + " "); int n3[] = { };
System.out.println(); vaTest(n1); // 1 arg
} vaTest(n2); // 3 args
vaTest(n3); // no args
}
}
L.Sumathi ,AP/CSE ,GCT 5
Varargs
A variable-length argument is specified by three periods (...).
// Demonstrate variable-length arguments.
class VarArgs { public static void main(String args[])
// vaTest() now uses a vararg. {
static void vaTest(int ... v) { // Notice how vaTest() can be called with a
System.out.print("Number of args: " + v.length // variable number of arguments.
+ vaTest(10); // 1 arg
" Contents: "); vaTest(1, 2, 3); // 3 args
for(int x : v) vaTest(); // no args
System.out.print(x + " "); }
System.out.println(); }
}
A method can have “normal” parameters along with a variable-length parameter. However,
the variable-length parameter must be the last parameter declared by the method.
L.Sumathi ,AP/CSE ,GCT 6
Inner Class
Nested Class : a class within a class
The purpose of nested classes is to group classes that belong together,
which makes code more readable and maintainable.
To access the inner class, create an object of the outer class, and then
create an object of the inner class:
public class Innerclassdemo {
public static void main(String[] args) {
class OuterClass { OuterClass myOuter = new OuterClass();
int x = 10;
1. InnerClass myInner = new InnerClass();
class InnerClass { 2. OuterClass.InnerClass myInner =
int y = 5; myOuter.new InnerClass();
}
}
System.out.println(myInner.y + myOuter.x);
}
}
L.Sumathi ,AP/CSE ,GCT 7
Check it
Can Methods with var arguments overloaded?
L.Sumathi ,AP/CSE ,GCT 8
Reference
https://www.javatpoint.com/
https://nptel.ac.in/noc/courses/noc20/SEM1/noc20-cs08/
➢Herbert Schildt, “Java, The Complete Reference “,
Eleventh Edition
➢geeksforgeeks.org/differences-jdk-jre-jvm/
➢https://www.codejava.net/java-se/java-se-versions-
history
L.Sumathi ,AP/CSE ,GCT 9