• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generics Question  RSS feed

 
Johnathan Davis
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between these 3 ArrayLists? I'm confused about whether they do the same thing or different things.

1. List myList = new ArrayList();
// I believe that this would be fully untyped, so we could put in Integers and Strings and anything that we wanted.

2. List<?> myList = new ArrayList<?>();
// Does this let us put in anything as long as all entries are the same type? (i.e. - all Integers, all Strings, etc...). Or is this equivalent to #1?

3. List<Character> result = new ArrayList<>();
// I'm confused about how we can set a List<Character> equal to a list of <>. I found this in a book and was shocked that the java compiler accepts it!

Thanks for the explanation!
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Option #1 is not using generics. It is allowed for backward compatibility reasons. And the compiler should be warning you about it.

Option #2 is a wildcard reference declaration. I believe you should be getting a compile error, as wildcards are for references, and not for object instantiations.

Option #3 is a diamond operator. It is just a shortcut, meaning you don't have to retype the generic type. The type specified earlier in the declaration part of the expression is implied.

Henry
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry is correct that #2 doesn't compile. Also note that #3 only compiles in Java 7 or later. So you might see references to it not compiling in older books.
 
Campbell Ritchie
Marshal
Posts: 56521
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnathan Davis wrote:. . .
3. List<Character> result = new ArrayList<>();
. . .
The compiler will try to make the type of the ArrayList equal to the type of its declaration. If it can infer the type, it will assume that means the same as
List<Character> result = new ArrayList<Character>();
It does not mean a list with no type or anything like that; it is all right and there is no need to be shocked. If you look in the Java™ Tutorials (scroll down about 70% of the page) you find that <> is called the diamond and is only permissible in Java7+.
 
salvin francis
Bartender
Posts: 1649
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnathan Davis wrote:...whether they do the same thing or different things.


As mentioned above, only 2 are legal code, here is some more information that would surprise you...

Generics is applied only at compile time !! Once you create a class file using the java compiler, all generics code is removed/erased.
You can find more information about how the erasure happens here : http://docs.oracle.com/javase/tutorial/java/generics/erasure.html
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!