How raw type works in Java? [duplicate]

问题: This question already has an answer here: What is a raw type and why shouldn't we use it? 15 answers I'm learning about generics and I have a misun...

问题:

This question already has an answer here:

I'm learning about generics and I have a misunderstanding.

For example I have this simple code:

import java.util.ArrayList;

public class DemoApp {

    public static void main(String[] args) {
        ArrayList list = new ArrayList();
        list.add(1);
        System.out.println(list.get(0).getClass().getName()); 
        int value = (Integer) list.get(0);
    }
}

I don't understand why should I cast the list.get(0) to Integer because this statement System.out.println(list.get(0).getClass().getName()); has this result java.lang.Integer?

If I use this statement int value = list.get(0); I get this error: Type mismatch: cannot convert from Object to int... and I really don't understand, is list.get(0) an Object or an Integer?


回答1:

The list.get(0).getClass().getName() will access an element in the List, and find out at runtime that it's indeed an integer. It could be anything else, though.

What the compiler knows at compile time is that your list contains items of type Object, see the signature of List.get(int). So it can't prove that int value = list.get(0) would always be correct. You have to explicitly cast.

The whole generics thing in Java is compile-time only. It's a way to tell the compiler how to limit type-parametrized classes only to particular types, and thus guarantee at compile time that operations on them are indeed correct. That is, the compiler won't allow you to put a String into a List<Integer>.

In runtime, the type parametrization information is erased. The JVM operates on by relying on the compiler having done the job of generating correct code. There's nothing at runtime that allows the JVM to know what type the content of a List is; ArrayList has a single implementation for any class parameters. (This is unlike C# or C++.)


回答2:

Because when you use raw type (like ArrayList without any generic type) Java assumes that the type of elements in that list is Object. So list.get(0) will return reference of type Object. And because of that you need to cast.

Object returned by list.get(0) is of type Integer but reference to it is of type Object and that's why compiler complaint because you want to assign reference of type Object to a variable of primitive type int.


回答3:

Plain ArrayList means ArrayList<?>, which means an ArrayList containing objects of unspecifed type. Since all objects derive from Object, the best you can say about those objects is that they're Objects.

The fact that, at run time a particular Object might turn out to be an Integer (as well) does not void the problem that at compilation time the object type is not known.

Consider:

list.add(1);
list.add("two");
for (int i=0; i<list.size(); i++)
    System.out.println(list.get(i).getClass().getName()); 

Now what is the type of list.get(i) ?

My question would be: why are you using raw types?


回答4:

ArrayList of raw type. It acts like an ArrayList<Object>. You can add all sub-types of object to it (and this is almost all).

So, if you take an object back from list, then it is a Object - at compile-time you cant know if it a Integer or String.

You dont need the cast when use a ArrayList<Integer>.

Edit:

import java.util.ArrayList;

public class DemoApp {

    public static void main(String[] args) {
        ArrayList list = new ArrayList();
        list.add(1); // an int
        list.add("Test"); // an String
        list.add(new Object()); // and empty

        var rnd = RandomUtils.random(list.size());

        for (Object value : list)      
           System.out.println(value.getClass().getName()); 


       list.get(rnd) // so, what is the type of the object?
       // the compiler cannot know it (compile-time), you need to run the program and check it (runtime)

    }
}

Edit2: See also https://docs.oracle.com/javase/tutorial/java/generics/rawTypes.html

  • 发表于 2019-03-30 01:01
  • 阅读 ( 210 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除