Does Java support passing variables by reference? [double]

this question already has answers here : in Java are arguments passed by value or by reference? (2 responses) Closed 2 years ago .

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

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

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

In the code above I already knew that I was not going to modify a in any respect 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 doubt, because from the Class anyone can mock directly to the other classes? (view.setTitle("View sería un jframe")) and from the first code I showed I can't modify the int?

 12
Author: MatiEzelQ, 2016-02-16

3 answers

First of all...

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

You will say but I 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 a address or identifier of the object in the dynamic memory space. When you parameter the variable, 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 them by parameters. a copy of the passed objects has not been created.

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

v = null;

You will only make the parameter v no longer store the address of the object of type Ventana. In the Class otraCualquiera, the 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 is allowed by reference. What you have to understand is that in case of objects the value that a variable stores is an address or identifier of the object and not the object itself.


If what you want is to find a way to be able to modify a variable of primitive type (like an int) from another method you pass it to (something not very common, but useful in some recursive algorithm), one technique could be to have the integer as a single element array, so it would be treated as an object and full reference).

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

 19
Author: Uexkull, 2016-02-16 15:45:29

The short points to remember here are that in Java:

  1. there is only the step by values.
  2. references to objects are values.
  3. 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 an everyday simplification-a convenient inaccuracy. If we want to get to the bottom of this question we have to understand that in the way 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 you have to be careful of which operations affect which and in which way. For example:

  1. the assignment (e.g., saludos = "¡Saludos de nuevo!";) changes the value of the variable. But the value of the variable in this case it is a reference, not an object. Mapping does not access objects-it works strictly with variables and their values.
  2. comparisons of variables, such as a == b or a != b, look at the current values of the variables. When these values are references, then, they observe these references but do not access their objects.
  3. null is a reference, not an object. It is a reference with no object, by definition.
  4. the invocation of methods (e.g., saludo.length()) or member reference (objeto.variable) observes the current value of the variable, and accesses the object corresponding to this reference.
  5. 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 strict application of these rules. For example:

The main doubt, because from the Class anyone can moficicar directly to 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're actually doing is:

  1. access the reference that is value of the variable view;
  2. access the subject of this reference;
  3. Lamar the method setTitle of this object, passing a reference to the object "View sería un jframe".

This in itself it does not modify anything; if there is modification it is indirectly, because the setTitle method directly modifies something or indirectly causes such modification by calls to 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 have the" power " to modify anything directly. Although saying as you say view.setTitle("View sería un jframe") "modify view" is a popular and simplified form of describe what happens, actually this is not what happens when you look strictly at how things work.

 4
Author: Luis Casillas, 2016-02-16 19:38:45

There are no references in Java. Parameters are passed by copy (variables of basic type of the language or by copying the address where they are hosted) this is not step by reference !!!!!!!. Passing by reference is to create an alias of a variable or object that has a different name but references the same memory space. For example:

Memory space 10 - > we have the person1 object saved in this space The reference would be - > create a person reference object that is an alias of person1.

In conclusion a reference is like calling a memory space in various ways without using additional memory resources.

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

 1
Author: Federico Manzano, 2017-03-05 21:33:15