Skip to content

Instantly share code, notes, and snippets.

@jerolba
Last active December 31, 2022 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jerolba/c8e5626d3886d2f986a7f8f159eab5a4 to your computer and use it in GitHub Desktop.
Save jerolba/c8e5626d3886d2f986a7f8f159eab5a4 to your computer and use it in GitHub Desktop.
Arrays are passed by value
public class ObjectArrayParameterByValue {
public static void main(String[] args) {
String[] data = { "Foo", "Bar" };
System.out.println(data[0]);
}
public void changeArray(String[] data) {
data[0] = "Changed";
}
}
@jmiguelr
Copy link

No acabo de pillar el punto de lo que haces aqui... :-) ¿No llamas a changeArray() (que debería ser static tambien) para cambiar el valor?

@jerolba
Copy link
Author

jerolba commented Dec 31, 2022

Olvidalo, estoy espeso... en un código que tocaba estaba mal referenciado el array (por eso no cambiaba) y luego al hacer el ejemplo sencillo me olvido de llamar al método.
He borrado el tweet porque estaba bien mi creencia: se pasa por referencia 🤦

@jmiguelr
Copy link

:-D He pillado justo el tweet en el minuto que ha estado vivo :-D .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment