I am curious to know if this works like I think it does. I do realize that generics just end up as Object at run time. This just an issue I ran into in Eclipse. I found this construct when looking how Java solved a casting issue for generics in Collections.emptyList(). I was getting tired of suppressing warnings when I was casting a list off of any interface I was using.
<T> List<T> emptyList()
It looks to me like it is reading the generic cast on assignment. For instance, any of these work without throwing any kind of casting errors.
List<Object> list = Collections.emptyList();
List<MyObect> list2 = Collections.emptyList();
Can anyone point me to the documentation on how this works. I am not even sure what to call this to search for it.
List<MyObject> list2 = new LinkedList<MyObject>();It creates an empty (linked) list. Check the tutorial page for collections: docs.oracle.com/javase/tutorial/collections/index.htmlCollections.emptyList()always returns a reference to the same list (it's immutable) therefore saving memory and overhead. But that really has nothing to do with his question, of course.