Difference between changing a variable and a recurring object

Asked

Viewed 54 times

3

I’m recurring in two situations: one changes an object field and the other directly changes the string. What is the difference between these two forms of return since it gives different result?

public class Person {
    String fname = "f";
    String lname = "Doe";
    int age = 24;

  public static  String mudaNome(String nome){
    if(!nome.equals("fiii")){
      nome = nome + "i";
      System.out.println("name1: " +nome);
      mudaNome(nome);
    }
    System.out.println("name1 output: " +nome);

   return nome;
}

public static Person mudaNomeObj(Person myObj){
    if(!myObj.fname.equals("fiii")){
      myObj.fname = myObj.fname + "i";
          System.out.println("name2 : " +myObj.fname);
      mudaNomeObj(myObj);
    }
    System.out.println("name2 output: " +myObj.fname);

  return myObj;
}


public static void main(String[] args) {
   Person myObj = new Person();

   System.out.println("name1 final output:" +mudaNome(myObj.fname));

   System.out.println("---------------");

   Person myObj2 = new Person();
   System.out.println("name2 final output:" + mudaNomeObj(myObj2).fname);
 }
}

Exit:

name1: fi
name1: fii
name1: fiii
name1 output: fiii
name1 output: fiii
name1 output: fii
name1 output: fi
name1 final output:fi
---------------
name2 : fi
name2 : fii
name2 : fiii
name2 output: fiii
name2 output: fiii
name2 output: fiii
name2 output: fiii
name2 final output:fiii

1 answer

3


The difference is precisely the local variable and the field that is used in the return.

The second example returns at the end the object field. Each execution of the function changes the field. The field is not local, it belongs to the object so every change the function makes to it is preserved, so at the end when it returns the field takes the state of the last change.

A local variable exists only in the context of that function at the time it is running, so at the end of its execution it is discarded and only survives a value that is returned at the end.

In the first example the return is only the result given in the first execution of the function, all the others are discarded. The reason for discarding is that the return is not used in any function context, note is called mudaNome(nome) and nothing is saved, so all the recursion is wasted except for the fact that it is printing something, which is probably there for debugging purposes, if you take that out the recursion is serving for nothing.

If you store the returned data and use it there it keeps state, despite a very inefficient way of doing. Or you don’t even need the variable, maybe what you wanted was:

class Person {
    String fname = "f";
    String lname = "Doe";
    int age = 24;

    public static String mudaNome(String nome) {
        if (!nome.equals("fiii")) nome = mudaNome(nome + "i");
        return nome;
    }

    public static Person mudaNomeObj(Person myObj) {
        if (!myObj.fname.equals("fiii")) {
            myObj.fname += "i";
            mudaNomeObj(myObj);
        }
        return myObj;
    }

    public static void main(String[] args) {
       Person myObj = new Person();
       System.out.println("name1 final output: " + mudaNome(myObj.fname));
       System.out.println("---------------");
       Person myObj2 = new Person();
       System.out.println("name2 final output: " + mudaNomeObj(myObj2).fname);
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Great. Thank you, bro!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.