Bifalgorithm post · Computer science one · Passing By Reference

How does Java handle passing primitive and object types to methods?

How does Java handle passing primitive and object types to methods? (Computer science one · Passing By Reference) — "Java does not allow you the ability to spe…

Ideas in this flowchart

  • Java distingue el comportamiento de los tipos primitivos y de los objetos al pasarlos a métodos.
  • Java no permite especificar explícitamente si una variable se pasa por referencia o por valor.
  • Todos los tipos primitivos se pasan por valor.
  • Todos los tipos de objetos se pasan por referencia.
  • Muchos tipos integrados, como Integer y String, son inmutables.
  • Un método que recibe un objeto inmutable no puede modificarlo.
  • Un método solo puede modificar el objeto recibido si este es mutable.
  • StringBuilder es un objeto de cadena mutable.
  • El método append() modifica el contenido de StringBuilder agregando texto al final.
  • El código compara el comportamiento de String y StringBuilder al añadir " world!".
  • La operación s = s + " world!" crea una nueva cadena y cambia la referencia del parámetro s.
  • La referencia a la cadena original "Hello" se pierde y se reemplaza por la nueva cadena.
  • La instancia de StringBuilder se modifica mediante append(), pero sigue siendo el mismo objeto.
  • En main se crean un objeto String y un objeto StringBuilder, ambos inicializados con "Hello".
Feed/

How does Java handle passing primitive and object types to methods?

No cover

Channel

Computer science one

1 page

"Java does not allow you the ability to specify if a variable is passed by reference or by value. Instead, all primitive types are passed by value while all object types are passed by reference. Moreover, most of the built-in types such as Integer and String are immutable, even though they are passed by reference, any method that receives them cannot change them. Only if the passed object is mutable can the method make changes to it (by invoking its methods). As an example, consider the following piece of code. The StringBuilder class is a mutable string object. You can change the string contents stored in a StringBuilder by calling one of its many methods such as append() , which will add whatever string you give it to the end. In the main method, we create two objects, a String and a StringBuilder and pass it to a method that makes changes to both by appending \" world!\" to them. Understand what happens here though. The first line in change() actually creates a new string and then changes what the parameter variable s references. The reference to the original string, \"Hello\" is lost and replaced with the new string. In contrast, the StringBuilder instance is actually changed via its append() method but is still the same object.\n\n1 public class Mutability {\n\n2\n\n3 public static void change(String s, StringBuilder sb) {\n\n4 s = s + \" world!\";\n\n5 sb.append(\" world!\");\n\n6\n\n7 System.out.println(\"change: s = \" + s);\n\n8 System.out.println(\"change: sb = \" + sb);\n\n9 }\n\n10\n\n11 public static void main(String args[]) {\n\n12 String a = \"Hello\";\n\n13 StringBuilder b = new StringBuilder(\"Hello\");\n\n14\n\n15 System.out.println(\"main: s = \" + a);"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.