Galera estou com algumas dúvidas em relação aos wrappers
Integeri=newInteger(10);// == compara somente o padrão de bits entre dois objetos distintos de mesmo tipo. Integers=newInteger(10);// Como são dois objetos distintos, cada um tem sua própria referência ok ok Integeri2=200;// issoIntegers2=200;// não é igual a issoif(i2==s2){System.out.println("iguais");}else{System.out.println("não iguais");}--------------+---------------------Integeri2=20;// Mas issoIntegers2=20;// é igual a isso!if(i2==s2){System.out.println("iguais");}else{System.out.println("não iguais");}
Porque quando uso 20 funfa e quando uso 200 não funfa ?!
Alguém tem um mateiral para me indicar ?!
Integer i1 = 1000;
é convertido pelo compilador em
Integer i = Integer.valueOf (1000);
não em
Integer i = new Integer (1000);
Se você ler o código de valueOf (fica como exercício), vai ver que se o valor passado como parâmetro for pequeno (-128 <= x <= +127), então ele pega de um array que contém já esses objetos Integer pré-alocados. Se for grande (como 200, por exemplo), então ele faz um “new Integer” mesmo.
Gustavo_Santos
valueOf() não retorna um objeto do tipo chamado novinho ?!
me manda um link de um material brother entanglement, não ficou claro ainda…
E
entanglement
O material está na sua máquina. Se você tiver o JDK, tem um arquivo src.zip. No arquivo java/lang/Integer.java, dentro desse zip, está o fonte da classe java.lang.Integer.
Para economizar sua viagem, vou postar o código relevante:
privatestaticclassIntegerCache{staticfinalinthigh;staticfinalIntegercache[];static{finalintlow=-128;// high value may be configured by propertyinth=127;if(integerCacheHighPropValue!=null){// Use Long.decode here to avoid invoking methods that// require Integer's autoboxing cache to be initializedinti=Long.decode(integerCacheHighPropValue).intValue();i=Math.max(i,127);// Maximum array size is Integer.MAX_VALUEh=Math.min(i,Integer.MAX_VALUE--low);}high=h;cache=newInteger[(high-low)+1];intj=low;for(intk=0;k<cache.length;k++)cache[k]=newInteger(j++);}privateIntegerCache(){}}