1

I can't actually see how Java manages the object's references passing. I expose this situation to tell my doubt.

public class Clase {


    public void metodo (int i, Integer objeto) {

        i++;
        objeto++;
    }
}

public class Main {

    public static void main(String[] args) {

        int         i = 5;
        Integer     object = new Integer(8);

        System.out.println(i);
        System.out.println(object);

        new Clase().metodo(i, object);

        System.out.println(i);
        System.out.println(object);


    }

}

All I'm getting printed is:

5
8
5
8

So, as long as I'm receiving in the method a primitive and an object, shouldn't the object be changing from the method too?

diegoaguilar
  • 8,179
  • 14
  • 80
  • 129

1 Answers1

3

Example object you used (Integer) is immutable object (which is same like String). Try same example with non immutable objects, you will get answer as you expected.

kosa
  • 65,990
  • 13
  • 130
  • 167