File tree 1 file changed +43
-0
lines changed
Roadmap/05 - VALOR Y REFERENCIA/java
1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change
1
+ public class hernanR {
2
+ public static void main (String [] args ) {
3
+ // Por valor: tipos primitivos
4
+ int a = 10 ;
5
+ int b = a ;
6
+ b = 20 ;
7
+ System .out .println ("Por valor - a: " + a );
8
+ System .out .println ("Por valor - b: " + b );
9
+
10
+ // Por referencia: objetos
11
+ int [] c = { 1 , 2 , 3 };
12
+ int [] d = c ;
13
+ d [0 ] = 10 ;
14
+ System .out .println ("Por referencia - c[0]: " + c [0 ]);
15
+ System .out .println ("Por referencia - d[0]: " + d [0 ]);
16
+
17
+ // DIFICULTAD EXTRA (opcional):
18
+ int [] valores = { 10 , 20 };
19
+ intercambiarPorReferencia (valores );
20
+ int nuevoX = valores [0 ];
21
+ int nuevoY = valores [1 ];
22
+ System .out .println ("Intercambio por referencia - nuevoX: " + nuevoX );
23
+ System .out .println ("Intercambio por referencia - nuevoY: " + nuevoY );
24
+ }
25
+
26
+ // Por valor
27
+ public static void porValor (int a , int b ) {
28
+ a = 20 ;
29
+ b = 30 ;
30
+ }
31
+
32
+ // Por referencia
33
+ public static void porReferencia (int [] c , int [] d ) {
34
+ c [0 ] = 10 ;
35
+ d [0 ] = 20 ;
36
+ }
37
+
38
+ public static void intercambiarPorReferencia (int [] arr ) {
39
+ int temp = arr [0 ];
40
+ arr [0 ] = arr [1 ];
41
+ arr [1 ] = temp ;
42
+ }
43
+ }
You can’t perform that action at this time.
0 commit comments