Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

264
Vistas
Why does using raw type variables affect signatures without reference to type-parameters?

Looking into another question I bumped into this intriguing behavior of the 1.8.0_112 Sun-Oracle compiler (I have not tested with others):

import java.util.List;

interface Alpha<T> {
   List<Integer> intList();
}

interface Beta {
   List<Integer> intList();
}

class Main {

   public static void main(String[] args) {

      Alpha rawAlpha = null;
      Alpha<Character> charAlpha = null;
      Alpha<?> qmAlpha = null;
      Beta beta = null;

      for (Integer i : charAlpha.intList()) {}
      for (Integer i : qmAlpha.intList()) {}
      for (Integer i : beta.intList()) {}
      for (Integer i : rawAlpha.intList()) {}
   }
}

The compiler only fails at the last for loop:

error: incompatible types: Object cannot be converted to Integer
      for (Integer i : rawAlpha.intList()) {}
                                       ^
1 error

So despite that intList() return list type List<Integer> in Alpha does not depend on the type parameter T, it seems that the <Integer> is erased at compilation time.

Notice that if we declare a non-generic interface Beta that would be, in theory, equivalent to making reference to the raw Alpha, there is no issues.

Is this the expected behavior? can some one point out the paragraph on the language spec that would cover this point? If this is not a bug at the very least it seem rather anti-intuitive and non-productive; perhaps is done for the sake of back-comparability?.

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The JLS bit that says this (a bit unclearly) is in JLS 4.8:

The type of a constructor (§8.8), instance method (§8.4, §9.4), or non-static field (§8.3) of a raw type C that is not inherited from its superclasses or superinterfaces is the raw type that corresponds to the erasure of its type in the generic declaration corresponding to C.

So, since rawAlpha is a raw type, the type of rawAlpha.intList is the erasure of List<Integer> intList(). That erasure is List intList().

As for why, I don't have a citation handy, but raw types are only really in Java for backwards compatibility. That means they only need to work as well as they did before generics; what you're asking for is for code that works just a little bit better that it used to. It's not unreasonable, but it's not what they decided on. :-)

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda