Eligible for GC

7 respostas
GustavoLaguna

Exercício do livro que não entendi a resposta.

Given:
class CardBoard {
Short story = 5;
CardBoard go(CardBoard cb) {
cb = null;
return cb;
}
public static void main(String[] args) {
CardBoard c1 = new CardBoard();
CardBoard c2 = new CardBoard();
CardBoard c3 = c1.go(c2);
c1 = null;
// do Stuff
} }

O objeto c3 não seria também?

7 Respostas

M

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.
Short story = 5;
// O compilador transforma o autoboxing nisso:
Short story = 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>.
     */
    public static Short valueOf(String s) throws NumberFormatException {
	return valueOf(s, 10);
    }

    private static class ShortCache {
	private ShortCache(){}

	static final Short cache[] = new Short[-(-128) + 127 + 1];

	static {
	    for(int i = 0; i < cache.length; i++)
		cache[i] = new Short((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
     */
    public static Short valueOf(short s) {
	final int offset = 128;
	int sAsInt = s;
	if (sAsInt >= -128 && sAsInt <= 127) { // must cache 
	    return ShortCache.cache[sAsInt + offset];
	}
        return new Short(s);
    }

***** EDIT:

Aqui vai um programa de teste:

public class Teste {
    public static void main(String[] main) {
        Short a = 5;
        Short b = 5;
        System.out.println(a == b);
    }
}

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.

Criado 7 de abril de 2008
Ultima resposta 7 de abr. de 2008
Respostas 7
Participantes 4