I'm doing a function that prints the percentage of the process, which ends when the counter reaches the target amount, but always throws me the following error:
Exception in thread "main" java.lang.StackOverflowError at pruebas.Bucles.recursion(Bucles.java:88)
The error is printed many times, here I leave my class:
public class Bucles {
private static final long META = 1000000000;
private long porcentaje;
public void probar(){
porcentaje = META / 100;
recursion(0);
}
private void recursion(long i) {
// System.out.println(i);
if (i < META) {
if (i > porcentaje && i % porcentaje == 0) {
System.out.println("proceso recursión : " + i / porcentaje + " %");
}
recursion(i++);// <----- Está es la linea 88
}
}
}
I made the change as they indicate me to change the postincremento:
private void recursion(long i) {
// System.out.println(i);
if (i < META) {
if (i > porcentaje && i % porcentaje == 0) {
System.out.println("proceso recursión : " + i / porcentaje + " %");
}
recursion(++i);// <----- Está es la linea 88
}
}
If I print the value of i
(I decompose the line) something very strange happens: