Generics

1 resposta
Daniel_Quirino_Olive

Estou meio por fora de como anda a implementação de Generics para o Tiger, mas hoje eu li algo que me deixou com uma dúvida:

Meanwhile, Sun has proposed the addition of generics in the next version of the Java language, codenamed Tiger. Sun has chosen an implementation that does not require modifying the Java Virtual Machine. As such, Sun is faced with implementing generics on an unmodified virtual machine.

The proposed Java implementation uses similar syntax to templates in C++ and generics in C#, including type parameters and constraints. But, because it treats value types differently than reference types, the unmodified Java Virtual Machine will not be able to support generics for value types. As such, generics in Java will gain no execution efficiency. Indeed, the Java compiler will inject automatic downcasts from the specified constraint, if one is declared, or the base Object type, if a constraint is not declared, whenever it needs to return data. Further, the Java compiler will generate a single specialized type at compile-time that it will then use to instantiate any constructed type. Finally, because the Java Virtual Machine will not support generics natively, there will be no way to ascertain the type parameter for an instance of a generic type at run-time and other uses of reflection will be severely limited. (http://www.gotdotnet.com/team/csharp/learn/Future/VCS%20Language%20Changes.aspx)

Bom, supondo que o texto acima não esteja falando nenhuma besteira, o suporte de generics a primitivos (int, double, boolean…) não poderia ser muito bem feito através da implementação de Autoboxing que também vai ser lançado no Tiger?

1 Resposta

louds

Não.
Porem com Autoboxing voce tem como usar primitivos em classes genéricas especialisadas para os wrappers.

Vector<Integer> x = new Vector<Integer>();
x.add(1);
int a = x.get(0);

Java não possui generics/templates da mesma forma que outras linguagens. Da forma como a Sun tá fazendo o negocio todo presta apenas para criar código com infinitamente menos casts e uso mais seguro de Collections e afins

Criado 26 de agosto de 2003
Ultima resposta 26 de ago. de 2003
Respostas 1
Participantes 2