What are Raw Types?

15

Reading and studying a bit about Kotlin, I found the following statement:

  

Raw are a big problem, but for reasons of   compatible they had to be maintained in Java, but Kotlin   for being a new language does not have this problem anymore.

What exactly are Raw Types ? And why are they a big problem?

    
asked by anonymous 17.08.2017 / 16:29

1 answer

17

What exactly are Raw Types?

According to language specification , in free translation:

  

More precisely, a crude type is defined as one of the   following:

     
  • The reference type that is formed by taking the name of a declaration of   generic type without a tracking type argument list.

  •   
  • An array type whose element type is a raw type.

  •   
  • A non-static member type of a raw type R that is not inherited from   a superclass or superinterface of R.

  •   

The term Raw Types became known with the introduction of Generics .

For example, I'll use Framework Collections :

When Collections was designed in Java version 1.2, a collection did not specify which type of object it would manipulate, that is, the collection could receive any type, as in the example:

List lista = new ArrayList();
lista.add(1);
lista.add("teste");

Manipulating an unbalanced collection as it is is costly, because when you remove an element from the collection, you should assign it to its type, as well as being inconvenient, not safe. Failure to report a type / argument to List is known as a Raw Type, or in free translation, a crude type.

Solving the Raw Type:

To solve this problem, in version 1.5 of Java, Generics , with this, it was possible to inform one type of the collection, according to the example:

List<String> lista = new ArrayList<String>();
lista.add("teste_0");
lista.add("teste_1");

So, since the compiler knows the element type of the collection, the compiler can check that you have used the collection consistently and can insert the correct templates on the values that are being taken from the collection. >

And why are they a big problem?

In fact, Raw Types are a big problem, because even with the introduction of Generics it would not be convenient to remove Raw Types, since legacy applications need to continue working.

    
17.08.2017 / 18:11