How to pass values by reference in Java?

4

Gentlemen, one of the methods of class Array has method sort .

The sort method works like this:

Arrays.sort(vetor);

The vector itself is changed, I searched in many places and by what I understood this is passing value by reference.

In C we use pointers, but in Java? How does it work?

    
asked by anonymous 06.10.2017 / 23:16

1 answer

5

In Java, whenever you use a parameter of an object type (that is, it is not primitive), what is passed is a reference to the object (ie not a copy).

Already with primitive types, the passage occurs by value (that is, the parameter is copied).

By making an analogy with C, it's as if any variable other than a primitive type is actually a pointer (and that's exactly how it's implemented by the JVM).

    
06.10.2017 / 23:22