2

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.

5
  • I think the term you're looking for is "Type inference" Commented Mar 19, 2013 at 21:02
  • What's the point of creating an empty list calling a method? I would do something like this: 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.html Commented Mar 19, 2013 at 21:02
  • 1
    @Barranka Because Collections.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. Commented Mar 19, 2013 at 21:04
  • Compile errors happen at compile time, so it is irrelevant that generics are not at runtime. Commented Mar 19, 2013 at 22:59
  • @Barranka: Also, it saves you from having to name the type parameter explicitly on the right side. (Although on Java 7+, you can use the diamond operator to also not explicitly name it) Commented Mar 19, 2013 at 23:00

3 Answers 3

1

It is inferring the type of emptyList from the declaration of the variable to which it is assigned. If you wish to specify it yourself, you can use

Collections.<MyObject>emptyList();

This can be useful, for example, in the method Arrays.asList. The method allows you to write code like the following:

List<String> myList = Arrays.asList("a", "b", "c");

but if you have a String[], asList will give you a List<String[]> instead of a List<String>. You can override this as follows:

List<String> myList = Arrays.<String>asList(myStringArray);

More on type inference in generic methods here.

Sign up to request clarification or add additional context in comments.

Comments

0

The Java "Type Inference" documentation should answer most questions related to this.

Type inference is a Java compiler's ability to look at each method invocation and corresponding declaration to determine the type argument (or arguments) that make the invocation applicable. The inference algorithm determines the types of the arguments and, if available, the type that the result is being assigned, or returned. Finally, the inference algorithm tries to find the most specific type that works with all of the arguments.

To illustrate this last point, in the following example, inference determines that the second argument being passed to the pick method is of type Serializable:

static <T> T pick(T a1, T a2) { return a2; }
Serializable s = pick("d", new ArrayList<String>());

You can change

List<MyObect> list2 = Collections.emptyList();

to

List<MyObect> list2 = Collections.<MyObject>emptyList();

to make it explicit that <T> should bind to <MyObject>, though for a variable initializer, it should infer that automatically.

1 Comment

Thanks for that link and the explanation.
0

Not sure what you are asking exactly. If you want to perform casting without compiler nagging you with warnings, we can trick the compiler to do that

String string = "hello";
Number number = bruteCast(string);

@SuppressWarnings("unchecked")
public static <A,B> B bruteCast(A a)
{
    return (B)(Object)a;
}

of course you'll have to use it prudently, make sure every call to bruteCast() is indeed correct.

1 Comment

At that point you should probably just switch away from a strongly typed language.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.