Does Java support the passing of variables by reference? [duplicate]

9

When I pass an object to a method using a parameter, the object is passed by value or reference? The doubt comes by this code:

int a = 2;
cambiar(a);
System.out.println(a);

public static void cambiar(int c) {
    c = 10;
}

In the previous code I already knew that I would not modify a in any aspect unless the method returned an int and saved it. But in the following code that happens?

class otraCualquiera {
    Partidas partidas = new Partidas();
    Ventana ventana = new Ventana();
    Gestor gestor = new Gestor(ventana,partidas);
}

class culquiera {
    private Ventana view;
    private Partidas play;

    public Gestor(Ventana v, Partidas p) {
        view = v;
        play = p;
    }
}

The main question, because from any class can I directly direct the other classes? ( view.setTitle("View sería un jframe") ) and from the first code that I showed I can not modify the int ?

    
asked by MatiEzelQ 16.02.2016 в 02:54
source

3 answers

17

First of all ...

In Java there is no step by reference. The step by value (or by copy as some call) is mandatory.

You will say but when I pass an array by parameters and modify it from the method I pass it to, it changes, I am not passing a copy of the array

It seems that my argument fails, but I explain:

What you store in a non-primitive variable is not the object itself but an address or identifier of the object in the dynamic memory space. When you pass the variable parameters, you are passing a copy of that address.

The case you have proposed

You have created three objects of type Partidas , Ventana and Gestor .

The object Gestor stores in its attributes the address of the objects Partidas and Ventana that you have passed through parameters. A copy of past objects has not been created.

What you do have is a copy of the address of the objects. If you add the following line to the Manager builder just after the ones you already have:

v = null;

You will only make the v parameter no longer store the object address of type Ventana . In class otraCualquiera , variable ventana will still have the correct address.

Therefore, the step IS ALWAYS by copy of the value, unlike, for example, C or C ++, where the step by reference is allowed. What you have to understand is that in the case of objects the value stored by a variable is an address or identifier of the object and not the object itself.

If what you want is to find a way to modify a variable of primitive type (like an int) from another method to which you pass it (something not very common, but useful in some recursive algorithms), a technique could be have the whole as an array of a single element, so it would be treated as an object and you would only pass a copy of its address (a full-fledged reference).

The usual thought is to store it as an object Integer , but this class is immutable.

    
answered by 16.02.2016 / 04:43
source
3

The brief points that should be remembered here are that in Java:

  • Only pass through values.
  • References to objects are values.
  • Objects, however, are not values.
  • The second point is one that causes a lot of confusion. In Java, when we have the following situation:

    String saludo = "¡Saludos a todos!";
    

    ... we popularly say that the value of the variable is an "object," but this way of speaking is a daily simplification-a convenient imprecision. If we want to penetrate to the bottom of this question it is necessary to understand that in the way that Java works, the value of saludos is not the object, but a reference to the object. The variable, the reference and the object are three things related but apart, and we must carefully consider which operations affect which and in which way. For example:

  • The assignment (e.g., saludos = "¡Saludos de nuevo!"; ) changes the value of the variable. But the value of the variable in this case is a reference, not an object. The assignment does not access the objects-works strictly with the variables and their values.
  • Comparisons of variables, such as a == b or a != b , observe the current values of the variables. When these values are references, then, they observe these references but do not access their objects.
  • null is a reference, not an object. It is a reference without an object, by definition.
  • Method invocation (e.g., saludo.length() ) or member reference ( objeto.variable ) looks at the current value of the variable, and accesses the object that corresponds to this reference.
  • The passage of arguments (e.g., "blablala" .equals (greeting)) passes a copy of the value of the variable. In this case it is a reference. This does not access the object.
  • All the examples you give can be answered by the strict application of these rules. For example:

      

    The main question, because from any class can I directly direct the other classes? ( view.setTitle("View sería un jframe") )

    In this example you give, contrary to what you say, there is no direct modification of another class. What you are doing, in reality, is:

  • Access the reference that is the value of the variable view ;
  • Access the object of this reference;
  • Lamar the setTitle method of this object, passing a reference to the "View sería un jframe" object.
  • This in itself does not change anything; if there is modification, it is indirectly, because the setTitle method modifies something directly or indirectly causes such modification by calling other methods.

    If you look at my list again, point # 1 is the only mechanism that truly modifies the value of a variable. None of the other points has the "power" to modify anything directly. Although saying how you say that view.setTitle("View sería un jframe") "modify view " is a popular and simplified way to describe what happens, in reality this is not what happens when we look strictly at how things work.

        
    answered by 16.02.2016 в 20:20
    1

    There are no references in Java. The parameters are passed by copy (variables of basic type of language or by the copy of the address where they are hosted) THIS IS NOT A STEP BY REFERENCE !!!!!!!. The passage by reference is to create an alias of a variable or object that has a different name but that REFERS to the same memory space. For example:

    MEMORY SPACE 10 - > We have the object persona1 saved in this space The reference would be - > create a person personaReference that is an alias of person1.

    In conclusion, a reference is like calling a memory space in several ways without using memory resources in an additional way.

    In Java the copy is used. of the content of the variable or in the case of an object of the memory address in which it is hosted. In both cases, the content of the variable is copied to another variable OR the memory address of an object in another object (it is what is called a pointer in C), the difference is that Java handles it in a dedicated way, whereas in C it does the programmer directly.

        
    answered by 05.03.2017 в 20:45