Reputation: 169
Hi i am studying Kaplan selftest questions and answers. I am really stuck with one explanation about Unchecked conversion warning. It states
The following statement will not generate an unchecked conversion warning:
ArrayList list = new ArrayList<>();
but in the next paragraph, it states
The following statement will generate unchecked warnings:
ArrayList list = new ArrayList<>(); //generates a conversion warning
I checked in the eclipse it gives the following warning msg
ArrayList is a raw type. References to generic type ArrayList should be parameterized
Can someone help me? is it just a typo mistake or am i missing some thing. Thanks in advance.
Edit I think i didn't explain my question properly. Actually what i am looking for is "what is the right answer for answering such a question e.g. ArrayList list = new ArrayList<>(); generates unchecked conversion. True/False. " Because in the Kaplan selftest two different statements are given and i am a bit lost, though when i tried the code in Eclipse it doesn't generate Unchecked conversion warning but it gives Reference to generic type.. warning. So i am looking for your suggestion/advice. Thanks once again in advance
Upvotes: 1
Views: 337
Reputation: 441
You are talking about generic types.
in java 1.6 you can use the constructor with generics as follows
ArrayList<String> list = new ArrayList<String>();
In java 1.7 you can use the constructor with generics as follows
ArrayList<String> consumeLinks = new ArrayList<>();
The preceding statement creates an ArrayList that holds strings. A way to indicate the kind of objects a data structure like ArrayList holds.
If you are using ArrayList with an older version of Java, you’d write a constructor like this:
ArrayList list = new ArrayList();
There was no way to restrict the type. so the the generics was introduced.
Although you can still do this, generics make your code more reliable because they give the compiler a way to prevent you from misusing an ArrayList. If you attempt to put an Integer object in an ArrayList that’s supposed to hold String objects, the compiler fails with an error.
Think this will help.
Upvotes: 0
Reputation: 767
JDK7: Using the diamond operator for constructor type inference
ArrayList<Type> list = new ArrayList<>();
The use of the diamond operator simplifies the use of generics when creating an object. It avoids unchecked warnings in a program, and it reduces generic verbosity by not requiring explicit duplicate specification of parameter types. Instead, the compiler infers the type.
Using the diamond operator when the type is not obvious:
Type inference is supported in Java 7 and later, only if the parameter type for the constructor is obvious. For example, if we use the diamond operator without specifying a type for the identifier shown as follows, we will get a series of warnings:
List arrayList = new ArrayList<>();
arrayList.add("First");
arrayList.add("Second");
Compiling the program with –Xlint:unchecked, results in the following warnings:
... taman\diamond.java:29: warning: [unchecked] unchecked call to add(E) as a member of the
raw type ArrayList
arrayList.add("First");
where E is a type-variable:
E extends Object declared in class ArrayList
... \taman\diamond.java:30: warning: [unchecked] unchecked call to add(E) as a member of the raw type ArrayList arrayList.add("Second");
where E is a type-variable:
E extends Object declared in class ArrayList
2 warnings
These warnings will go away if the data type is specified as follows:
List<String> arrayList = new ArrayList<>();
Upvotes: 0
Reputation: 1679
For one: This is a warning, if you are careful you may ignore it and your code will still compile and run just fine.
However, since you want to get rid of it you should give your ArrayList
a type when defining it.
ArrayList<Type> list = new ArrayList<>();
This type can be any class, but not a primitive. If you need a primitive, use the wrapper classes Java provides.
Upvotes: 3