I thought I know Java well. We have an int - primitive type. When we transfer it to a method, the method does not refer to a variable, but only a copy. I assumed that Integer, the full-fledged class of packing int is passed to the method by reference, and any manipulations with the Integer variable passed to the method will occur directly from the variable we passed, and not by some copy.
I don't understand why the following code works against my understanding of types in Java.
Integer a=5; inc(a); System.out.println(a); private static void inc(Integer a){ a++; } Output:
5 Explain why 5, not 6 ???
a? In the method adopted, increased by 1, buta = 6is already a local variable in the method itself. - Pollux