Java - Primitive types and types by reference

6

I am learning Java and I have a question about the types of data.

A variable of primitive type stores a single value. When I assign a new one, the previous value is lost.

Now with the variables by reference I pose an example, I have a class called Auto , with an instance variable, called Color .

If I create two objects of that class, both would point "to the same" in memory, but each would keep an individual copy of the instance variable, so I can change the value of Color in an object, without affect the other. However, if I destroy the object, the references of both would be lost (I guess they would point to null again).

Is this concept that I have correct?

    
asked by FermatsTheorem 08.07.2016 в 17:21
source

2 answers

4

To complement the good response from @NaCl , I think this part is as doubt:

  

Now with the variables by reference I pose an example, I have a class called Auto, with an instance variable, called Color.

     

If I create two objects of that class, both would point "to the same" in memory, but each would keep an individual copy of the instance variable, so I can change the value of Color in an object, without affect the other. However, if I destroy the object, the references of both would be lost (I guess they would point to null again).

Let's design the classes Auto and Color :

//diseñada como inmutable puesto que un color no puede cambiar
public class Color {
    private final String name;
    public Color(String name) {
        this.name = name;
    }
    public String getName() { return this.name; }
    public String toString() { return getName(); }
}

public class Auto {
    private int year;
    private Color color;
    //constructor vacío
    //getters y setters para la clase
}

You can only create new instances of an object using the word new . When creating two instances by using new and assigning them to different variables, the variables will point to different memory locations. This can be seen in the image:

If we use the same instance of Color in both instances of Auto , the result will be as follows:

If you changed the color value in another of them, only the other would be affected:

    
answered by 08.07.2016 / 18:16
source
2

When you use a primitive type, when you assign a value to it, the previous value is "overwritten", so yes, it is literally lost, because variable values can change at any time.

When you work with objects, for example, let's say you have the class Auto :

class Auto
{
    // ...
}

And you assign a new instance:

Auto Toyota = new Auto(2005, "Camry"); // Año y Modelo

You have a new car placed in memory, if we do the following:

Auto OtroToyota = Toyota; 

The new variable OtroToyota points to the reference of the new instance that we created previously, which means that when changing any value in any of the instances, this value is changed in all the references that you have towards that object, example :

Toyota.Modelo = "Corolla"; 
System.out.print(OtroToyota.Modelo); // Debe imprimir "Corolla"
// O por lo menos este es el comportamiento estimado en C#, en Java debe parecerse.

So yes, your concept is true to the point that you point to the instance defined with the keyword new . If you assign null to one of the 2, the other (and all other pointers to that instance) will be affected.

See the following implementation as an example:

public class Auto {
    // Implementación de la clase Auto (ejemplo):
    public int Year;
    public Auto(int y) { Year = y; }
}

public static void main(string []args) {
    Auto Camry = new Auto(1994);
    Auto Toyota = Camry;
    System.out.println(Camry.Year);  // Imprime 1994
    Toyota.Yr = 1995;
    System.out.println(Camry.Year);  // Imprime 1995
    System.out.println(Toyota.Year); // Imprime 1995
}

Then, your answer finally summarized is "Yes", but "copies" of instances in pointers are not kept (Or at least that is what I understand) .

    
answered by 08.07.2016 в 17:43