-2

I found a problem when I am using java generics.

List list = new LinkedList<String>();
list.add(MyObject);  //No

prompts for any compilation errors.This is not I want

If I use :

List<String> list = new LinkedList<>();
list.add(MyObject); //Prompt for compilation errors.This is what I want

I wanna know what is the difference between

List<String> list = new LinkedList<>() and List list = new LinkedList<String>() and List<String> list = new LinkedList<String>() and List<String> list = new LinkedList()?

Saurabh
  • 882
  • 1
  • 5
  • 16
rockwe
  • 11
  • 4
  • hi guys , I found a problem when I am using java generics. List list = new LinkedList() list.add(MyObject) ,//No prompts for any compilation errors.This is not I want If I use : List list = new LinkedList<>() list.add(MyObject) ,//Prompt for compilation errors.This is what I want I wanna to know what is the difference List list = new LinkedList<>() vs List list = new LinkedList() vs List list = new LinkedList() vs List list = new LinkedList()? – rockwe Jan 24 '20 at 03:01
  • stackoverflow has a bug for post – rockwe Jan 24 '20 at 03:01
  • 4
    No it hasn't. Any `<` or `>` characters in normal markdown text need to be escaped or they are likely to be treated as unrecognized html elements and "disarmed". Solution: use back-quotes or code blocks. – Stephen C Jan 24 '20 at 03:06
  • 1
    Enable all compiler warnings and pay attention to them. The compiler will tell you why you cannot omit the `<…>`. – VGR Jan 24 '20 at 03:40

1 Answers1

3

List<String> list = new LinkedList<String>(); does the following:

  1. Declare a variable called list with type List<String>
  2. Call the constructor of LinkedList with the type parameter String
  3. Sets the value of list to the result of step 2.

Since a List<String> is obviously not going to be given a new LinkedList<Elephant>();, it is OK to remove the type parameter from the second part, giving: List<String> list = new LinkedList<>();. This is called "Type Inference". Java can only do this when it can calculate at compile-time what the omitted type would be.

If you use List list = new LinkedList<String>();, you do exactly the same thing as above, except your new variable list does not contain type information. This can be dangerous, as it prevents the compiler from warning/stopping you when you do something that would cause a type error.

For example:

List<String> list = new LinkedList<>();
list.add("hello");  // works fine
list.add(123);  // compile-time error

I have been saved from putting an int into a list of Strings. However, if using a regular List:

List list = new LinkedList<String>();
list.add("hello");  // stil works
list.add(123);  // also works

The issue with this comes from when you then retrieve items from the list:

List list = new LinkedList<String>();
list.add(123);  // allowed
Object obj = list.get(0);  // is obj a String? is it an int? 

This breaks type safety, which is arguably a strong reason for using Java in the first place.

cameron1024
  • 9,083
  • 2
  • 16
  • 36