Are generic type parameters converted to Object for raw types?
Consider this piece of code:
public class Main { public static void main(String[] args) { Cat cat = new Cat(); Integer i= cat.meow(); cat.var = 6; } } public class Cat { public E var; public E meow() { return null; } }
As per my understanding since I've not specified the type parameter on LHS, it would be taken as Object. And Cat should become Cat
actually compile is this:
Cat c1 = ... String c1Var = (String) c1.var;
As you can see, var is always treated as a field of type Object, but with generics, the compiler automatically inserts type-safe casts. That's all. If you use raw types, you have to do it yourself. Either way, when you put a Cat that stores an integer in a Cat variable, you will only get a runtime exception if you try to read var.
A quiz
Now look at the declaration of Collections.max. Why do you think the parameter is defined as T extends Object & Comparable?
Here is another good example that I just stumbled upon:
class Foo { public V bar(V v) { return v; } } //compiles Foo foo = new Foo(); Integer i = foo.bar(1); //compiles Foo foo = new Foo(); Integer i = foo.bar(1); // fails Foo foo = new Foo(); Integer i = foo.bar(1); // error: Object cannot be converted to Integer
Using no parameters disables generics entirely.
Answer by hetec for Are generic type parameters converted to Object for raw types?
I think Cat c is a RAW type and could be considered as a "wildcard type" like Cat. Since Cat is the supertype of each type of Cat including Cat, Cat c may take a new Cat object.
"Most people's first instinct is that Collection really means Collection. However, as we saw earlier, it isn't safe to pass a Collection in a place where a Collection is required. It's more accurate to say that the type Collection denotes a collection of some unknown type, just like Collection."
...
"So raw types are very much like wildcard types, but they are not typechecked as stringently. This is a deliberate design decision, to allow generics to interoperate with pre-existing legacy code."
Answer by Kshitiz Sharma for Are generic type parameters converted to Object for raw types?
@Cephalopod has provided the correct answer, however I'd like to expand on that with some of my own explanation.
for the variable declaration to make any sense T must translate to a Class/Interface reference.
That is correct. Generics are a compile time transformation. Runtime system has no notion of abstract types. So before the class is loaded into memory the abstract type T must be replaced by an actual type reference.
The formal type parameter E has been replaced with Object.
Cat should become Cat
Wrong. Cat will stay Cat. Why? Look at the decompiled class file for Main:
public class Main { public static void main(String[] args) { Cat cat = new Cat(); Integer i = (Integer)cat.meow(); cat.var = Integer.valueOf(6); } }
The purpose of specifying formal type parameter with <> is to enable compiler to generate explicit casts.
When you say new Cat() it doesn't have to turn into anything, the compiler simply won't generate a cast and the method call would look like:
Integer i = cat.meow(); // No cast at all
Are generic type parameters converted to Object for raw types?
To clarify what is being asked here, the above questions means: Is E replaced with java.lang.Object if I don't specify anything when instantiating Cat.
Actually E would be replaced with java.lang.Object even if you specified when instantiating Cat. The replacement/transformation is done at compile time while the instantiation is at runtime. How you use the type isn't going to change its class definition.
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72