Contenido de certificación
Buscar
Social
Ofertas laborales ES
« GregorianCalendar en Java | Main | Preguntas sobre fechas »
miércoles
abr022014

Pregunta sobre referencias

¿Cuál es la salida del siguiente programa y por qué? NOTA: no vale hacer uso de IDE.
01.package test;
02. 
03.import java.util.ArrayList;
04.import java.util.List;
05. 
06./**
07. * Probando referencias en objetos y en string's
08. *
09. * @author Jaime Carmona Loeches
10. *
11. */
12.public class Pruebas {
13. 
14.    /**
15.     * @param args
16.     */
17.    public static void main(String[] args) {
18.        List<objeto> lista1 = new ArrayList<objeto>();
19.        lista1.add(new Objeto(1, 1));
20. 
21.        List<string> lista2 = new ArrayList<string>();
22.        lista2.add("1");
23. 
24.        for (Objeto objeto : lista1) {
25.            objeto.setA(2);
26.            objeto.setB(2);
27.        }
28. 
29.        for (String objeto : lista2) {
30.            objeto = "2";
31.        }
32. 
33.        for (Objeto objeto : lista1) {
34.            System.out.println(objeto.toString());
35.        }
36. 
37.        for (String objeto : lista2) {
38.            System.out.println(objeto.toString());
39.        }
40.    }
41. 
42.}
43. 
44.class Objeto {
45.    int a = 0;
46.    int b = 0;
47. 
48.    public Objeto(int a, int b) {
49.        super();
50.        this.a = a;
51.        this.b = b;
52.    }
53. 
54.    public int getA() {
55.        return a;
56.    }
57. 
58.    public void setA(int a) {
59.        this.a = a;
60.    }
61. 
62.    public int getB() {
63.        return b;
64.    }
65. 
66.    public void setB(int b) {
67.        this.b = b;
68.    }
69. 
70.    @Override
71.    public String toString() {
72.        return "Objeto [a=" + a + ", b=" + b + "]";
73.    }
74. 
75.}
76. 
77.</string></string></objeto></objeto>

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>