Given:classCardBoard{Shortstory=5;CardBoardgo(CardBoardcb){cb=null;returncb;}publicstaticvoidmain(String[]args){CardBoardc1=newCardBoard();CardBoardc2=newCardBoard();CardBoardc3=c1.go(c2);c1=null;// do Stuff}}
Gustavo, c3 é apenas uma referência, nenhum objeto foi criado no heap.
victorwss
c3 não é porque c3 é nulo.
c1 é eligível.
Se c1.story é elegível, há controvérsias.
// Usa autoboxing.Shortstory=5;
// O compilador transforma o autoboxing nisso:Shortstory=Short.valueOf(5);
Short mantém um pool interno, será obtida a referência do pool. Daí a resposta certa seria a B.
GustavoLaguna
Hum, entendi…
Agora fica a duvida quanto a váriavel de instancia. Penso que como o objeto não poderá ser alcançado, referenciado… a sua variável de instancia também não, certo?
Omeganosferatu
Não entendi esse lance do pool … Não vejo problemas na resposta do livro
GustavoLaguna
Agora também não vejo. Valeu pela ajuda galera =)
victorwss
Omeganosferatu:
Não entendi esse lance do pool .... Não vejo problemas na resposta do livro
java.lang.Short:
/** * Returns a <code>Short</code> object holding the * value given by the specified <code>String</code>. The argument * is interpreted as representing a signed decimal * <code>short</code>, exactly as if the argument were given to * the {@link #parseShort(java.lang.String)} method. The result is * a <code>Short</code> object that represents the * <code>short</code> value specified by the string. <p> In other * words, this method returns a <code>Byte</code> object equal to * the value of: * * <blockquote><code> * new Short(Short.parseShort(s)) * </code></blockquote> * * @param s the string to be parsed * @return a <code>Short</code> object holding the value * represented by the string argument * @exception NumberFormatException If the <code>String</code> does * not contain a parsable <code>short</code>. */publicstaticShortvalueOf(Strings)throwsNumberFormatException{returnvalueOf(s,10);}privatestaticclassShortCache{privateShortCache(){}staticfinalShortcache[]=newShort[-(-128)+127+1];static{for(inti=0;i<cache.length;i++)cache[i]=newShort((short)(i-128));}}/** * Returns a <tt>Short</tt> instance representing the specified * <tt>short</tt> value. * If a new <tt>Short</tt> instance is not required, this method * should generally be used in preference to the constructor * {@link #Short(short)}, as this method is likely to yield * significantly better space and time performance by caching * frequently requested values. * * @param s a short value. * @return a <tt>Short</tt> instance representing <tt>s</tt>. * @since 1.5 */publicstaticShortvalueOf(shorts){finalintoffset=128;intsAsInt=s;if(sAsInt>=-128&&sAsInt<=127){// must cache returnShortCache.cache[sAsInt+offset];}returnnewShort(s);}
Imprime true. Ou seja a e b são o mesmo objeto, e não duas instâncias distintas.
Omeganosferatu
Ahh pode cre, achava que apenas Strings mantinham um pool… Não lembro de ter lido isso no Livro, mas ninguem melhor do que a própria API pra nos dizer, Valeu victorwss.