Galera andei lendo uns tópicos sobre wrap, reflection e tals…Olhe o codigo abaixo:
Field f = Integer.class.getDeclaredField("value");
f.setAccessible(true);
Integer two = 2;
f.set(two, 3);
System.out.printf("The value of two is now '%d' %n", two);
Integer x;
x = 2;
x = x + 2;
System.out.printf("The value of x is now '%d' %n", x); //5
f.set(x, 2);
System.out.printf("The value of x is now '%d' %n", x);//3
x = x + 2;
System.out.printf("The value of x is now '%d' %n", x); //3
f.set(x, 10);
System.out.printf("The value of x is now '%d' %n", x);//10
A minha dúvida é porque neste trecho:
f.set(x, 2);
System.out.printf("The value of x is now '%d' %n", x);//3
o valor de x não é mudado para 2?
Um chute…
Integer two = 2;
f.set(two, 3);
é… só que 2 virou 3
Agora não entendi por que nessa parte
x = x + 2;
System.out.printf("The value of x is now '%d' %n", x); //3
ele imprime 3, sendo que o valor de X virou x + 2
edit----
arrumado o code
Eu já li um tópico aqui no GUJ sobre o mesmo assuno, algo como 2 + 2 = 5 do thingol
http://www.guj.com.br/posts/list/133512.java é o tópico “2 + 2 = 5?” de que vocês se lembraram.
Foi neste tópico do thingol que me embasei…
Thingol você que criou aquele tópico me responda:
Field f = Integer.class.getDeclaredField("value");
f.setAccessible(true);
Integer two = 2;
f.set(two, 3);
System.out.printf("The value of two is now '%d' %n", two);
Integer x;
x = 2;
x = x + 2;
System.out.printf("The value of x is now '%d' %n", x); //5
f.set(x, 2);
System.out.printf("The value of x is now '%d' %n", x);//3 Porque aqui é impresso 3 e não 2?
x = x + 2;
System.out.printf("The value of x is now '%d' %n", x); //3
f.set(x, 10);
System.out.printf("The value of x is now '%d' %n", x);//10
Dá uma lida lá no tópico.
A explicação é que o 2, nesse ponto
Integer x;
x = 2;
não é um valor inteiro, mas sim um “ponteiro” para um objeto do tipo Integer que foi criado anteriormente e que teve valor 3 atribuído a ele.
O compilador transforma o seu código para o seguinte código equivalente:
Integer two = Integer.valueOf (2);
f.set(two, Integer.valueOf (3));
System.out.printf("The value of two is now '%d' %n", two);
Integer x;
x = Integer.valueOf (2);
x = Integer.valueOf (x.intValue() + 2);
System.out.printf("The value of x is now '%d' %n", x); //5
f.set(x, Integer.valueOf (2));
System.out.printf("The value of x is now '%d' %n", x);//3 Porque aqui é impresso 3 e não 2?
x = Integer.valueOf (x.intValue() + 2);
System.out.printf("The value of x is now '%d' %n", x); //3
f.set(x, Integer.valueOf (10));
System.out.printf("The value of x is now '%d' %n", x);//10
Acompanhe passo a passo e veja o que ocorre. É aconselhável usar um debugger para ver com atenção o que ocorreu.
[quote=thingol]O compilador transforma o seu código para o seguinte código equivalente:
Integer two = Integer.valueOf (2);
f.set(two, Integer.valueOf (3));
System.out.printf("The value of two is now '%d' %n", two);
Integer x;
x = Integer.valueOf (2);
x = Integer.valueOf (x.intValue() + 2);
System.out.printf("The value of x is now '%d' %n", x); //5
f.set(x, Integer.valueOf (2));
System.out.printf("The value of x is now '%d' %n", x);//3 Porque aqui é impresso 3 e não 2?
x = Integer.valueOf (x.intValue() + 2);
System.out.printf("The value of x is now '%d' %n", x); //3
f.set(x, Integer.valueOf (10));
System.out.printf("The value of x is now '%d' %n", x);//10
Acompanhe passo a passo e veja o que ocorre. É aconselhável usar um debugger para ver com atenção o que ocorreu.[/quote]
Esse esquema aí é bem bom pra fazer uma bela porcaria sem se dar conta.
É bem complexo esse processo, vou gastar um tempão depurando até entender hehe…Obrigado pela atenção de todos!
Qual a função desse objeto field (f)?