• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the difference between ArrayList and ArrayList<.....Any Class Here....>?

 
Vishal Kashyap
Ranch Hand
Posts: 73
BSD C++ Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Right now; I am studying Head First Java, and little bit confused about----

ArrayList and ArrayList<.....Any Class Here....>

as I know, ArrayList always comes with their angle brackets; shows, which class it was for.
but getting confused, because of its(ArrayList) use without angle brackets and class in it. at page.287 first column(and several places).


I'd like to know,

could we use ArrayList like that i.e. without angle brackets & their class? if yes, then what does it mean to?

As I think, if we don't use angle brackets; then it means ArrayList will use Object class. Isn't it?

Please, guide me, here.
Thanks.

 
Stephan van Hulst
Bartender
Pie
Posts: 5912
66
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any generic type (List, Set, Map, Class, Comparable, etc) that is not properly parameterized with angle brackets is known as a 'raw generic type'. The only reason they exist is because of backwards compatibility with older Java versions that did not have generics. Never use raw types.

Having said that, raw types function as if they were parameterized with Object. The difference is that you can assign any instance of a parameterized type to it:

Here is an example: ArrayList list = new ArrayList<Something>(). This stands in contrast with: ArrayList<Object> = new ArrayList<Something>(). This won't compile.

With raw types, the compiler won't do any checking for you, and you have to cast objects yourself. Don't use raw types. Ever.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See Generics for more information.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic