"Hai, Amigo. Dina iki Bilaabo bakal ngandhani babagan rekursi."

Kaya sing wis dingerteni, ing Jawa sawetara metode nyebutake metode liyane. Kajaba iku, nalika metode diarani, argumen tartamtu diterusake, nanging variabel lokal metode kasebut njupuk nilai tartamtu nalika mlaku.

"Uh-huh."

"Lan sampeyan ngerti, variabel internal metode sing beda-beda gumantung saka siji liyane."

"Uh-huh."

"Dadi mbayangno kahanan sing diarani metode kasebut. Iki diarani rekursi. Contone: "

``````public static void main(String[] args)
{
countDown(10);
}

public static void countDown(int x)
{
if (x <= 0)
System.out.println("Boom!");
else
{
System.out.println(x);
countDown(x - 1);
}
}``````
Output layar:
``````10
9
8
7
6
5
4
3
2
1
Boom!``````

"Aku bisa ndeleng manawa metode kasebut diarani kode kasebut, nanging aku ora ngerti apa sing kedadeyan."

"Ora, aku takon babagan apa sing kedadeyan karo variabel kasebut? Kanthi nilai-nilai kasebut? Lan kepiye cara metu saka metode kasebut? Utawa kita metu kabeh bebarengan?"

"Kabecikan sih-rahmat. Iku kabeh luwih prasaja. Mbayangno yen cara sing nyebut dhewe wis ping pirang-pirang. Banjur kita bakal duwe kahanan analog: "

Panggilan metode rekursif Apa pancene kedadeyan
``````public static void main(String[] args)
{
countDown(10);
}

public static void countDown(int x)
{
if (x <= 0)
System.out.println("Boom!");
else
{
System.out.println(x);
countDown(x - 1);
}
}``````
``````public static void main(String[] args)
{
countDown1(10);
}

public static void countDown1(int x)
{
if (x <= 0)
System.out.println("Boom!");
else
{
System.out.println(x);
countDown2(x - 1);
}
}
public static void countDown2(int x)
{
if (x <= 0)
System.out.println("Boom!");
else
{
System.out.println(x);
countDown3(x - 1);
}
}
public static void countDown3(int x)
{
if (x <= 0)
System.out.println("Boom!");
else
{
System.out.println(x);
countDown4(x - 1);
}
}

public static void countDown4(int x)
{
if (x <= 0)
System.out.println("Boom!");
else
{
System.out.println(x);
countDown5(x - 1);
}
}``````
Output layar: Output layar:
``````3
2
1
Boom!``````
``````3
2
1
Boom!``````

"Ing tembung liyane, saben metode diarani (malah dhewe), variabel anyar digawe sing nyimpen data kanggo metode iki. Ora ana variabel sing dienggo bareng."

"Kanthi saben telpon, salinan argumen metode liyane, kanthi nilai anyar, digawe ing memori. Nalika kita bali menyang metode lawas, variabel kasebut digunakake ing kana. Ing tembung liya, sajrone rekursi kita bener-bener nelpon metode liya, nanging kanthi kode sing padha karo kita! "

"Aku ngerti. Lan kepiye cara metu saka metode kasebut? Mungkin conto?"

"OK. Conto regane sewu tembung. "Iki conto sampeyan:"

Panggilan metode rekursif Panggilan metode rekursif
``````public static void main(String[] args)
{
print(3);
}

public static void print(int x)
{
if (x <= 0)
System.out.println("Boom!");
else
{
System.out.println(x);
print(x - 1);
System.out.println(x);
}
}``````
``````public static void main(String[] args)
{
print1(3);
}

public static void print1(int x)
{
if (x <= 0)
System.out.println("Boom!");
else
{
System.out.println(x);
print2(x - 1);
System.out.println(x);
}
}

public static void print2(int x)
{
if (x <= 0)
System.out.println("Boom!");
else
{
System.out.println(x);
print3(x - 1);
System.out.println(x);
}
}

…``````
Output layar: Output layar:
``````3
2
1
Boom!
1
2
3``````
``````3
2
1
Boom!
1
2
3``````

"OK. Aku rumangsa ngerti. Apa kita kudu rekursi?"

"Ana akeh, akeh tugas sing bisa dipérang dadi subtasks kapisah sing padha karo tugas asli. Contone, sampeyan kudu lumaku liwat kabeh unsur wit XML. Saben unsur bisa duwe sawetara unsur anak, lan padha duwe unsur anak dhewe."

"Utawa sampeyan kudu nampilake dhaptar file sing ana ing direktori lan kabeh subdirektori. Dadi sampeyan nulis metode sing nampilake file direktori saiki. Banjur kanggo entuk file kabeh subdirektori, sampeyan nelpon metode sampeyan nggunakake argumen sing beda: subdirektori."

Tampilake kabeh file ing direktori lan subdirektori
``````public static void main(String[] args)
{
printAllFiles(new File("c:/windows/"));
}

public static void printAllFiles(File dir)
{
for (File file : dir.listFiles())
{
if (file.isDirectory())
printAllFiles(file);
else
System.out.println(file.getAbsolutePath());
}
}``````

"Line 8 - Kita entuk dhaptar kabeh file (lan direktori) ing direktori dir."

"Baris 10-11 - Yen file iku bener direktori, banjur kita nelpon printAllFiles maneh, nanging wektu iki karo argumen liyane: subdirektori."

"Line 13 - Kita nampilake jeneng file saiki."

"Oke. Aku rumangsa ngerti. Matur nuwun, Bilaabo."