user1851725
user1851725

Reputation: 33

Why do I get compiler warnings with generics?

My code is:

import java.util.*;

public class A {
public static void main(String[] args){
    List<String> list = new ArrayList();
    list.add("1"); //ok   line 1
    list.add(1); //error  line 2
}

When I run this code Java gives me an error and I know why, but even when I only use line 1 the compiler warns me. Why do I get this warning? I don't understand what is the difference between my first example and this code:

import java.util.*;

public class A {
    public static void main(String[] args){
        List<String> list = new ArrayList<String>(); // <-- notice the second <String>
        list.add("1"); //ok   line 1
        list.add(1); //error  line 2
    }
}

Upvotes: 2

Views: 164

Answers (4)

PermGenError
PermGenError

Reputation: 46438

When I run this code Java gives me an error and I know why, but even when I only use line 1 the compiler warns me. Why do I get this warning?

When you use your code, compiler will give you a warning like

List<String> list = new ArrayList();

ArrayList is a raw type. References to generic type ArrayList should be parameterized

It says that you have to provide a parameterized type for your ArrayList as well.

List<String> list = new ArrayList<String>();

EDIT:

As sugguested by @newacct: if you are using Java 7, you could use empty angular brackets (<>) when instantiating your Collection like:

List<String> list = new ArrayList<>();

From the Java 7 documentation:

You can replace the type arguments required to invoke the constructor of a generic class with an empty set of type parameters (<>) as long as the compiler can infer the type arguments from the context.

Upvotes: 5

MC Emperor
MC Emperor

Reputation: 23057

Code 2 won't compile because you cannot add an int (or Integer) to a list with Strings, but you already knew that.

Speaking of code 1, there can be several reasons that the compiler will warn you, depending on what compiler you are using and what settings that compiler is using. Here are some options:

  • In construction of parameterized objects, you must also specify the parameter. In this piece of code: List<String> list = new ArrayList(), the construction of the ArrayList object does not have a parameter, which is, on the other hand, given in the reference. The compiler suggests that you use List<String> list = new ArrayList<String>() instead.
  • You are using redundant type arguments. When using the code List<SomeType> list = new ArrayList<SomeType>();, you have twice specified that the parameter should be of type SomeType. The compiler suggests that you use the diamond inference (new ArrayList<>()). That suggestion is used since Java version 1.7.

I guess your compiler is warning you about the first option above. However, my compiler does not give any warnings when testing your code.

Upvotes: 1

Vombat
Vombat

Reputation: 1166

This might help to understand the concept of generics: why use generics.

If you change your code as follows, there will be no errors and warnings:

import java.util.*;
public class A{

public static void main(String[] args){
    List<Object> list = new ArrayList<Object>();
    list.add("1"); //ok   code 1
    list.add(1); //error  code 2

}
}

Upvotes: 1

Less
Less

Reputation: 116

You have a List of type String so you cant put in Integers.

Upvotes: 0

Related Questions