• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Generics Question

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author & internet detective
Posts: 41250
849
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 76440
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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+.
 
Bartender
Posts: 2908
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
She's out of the country right now, toppling an unauthorized dictatorship. Please leave a message with this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic