• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Generics

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does List and List<Object> are same or different in java?
 
Marshal
Posts: 76872
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
List<?> is different too. It is pronounced, “List of unknowns,” or similar. It means you are going to have a specific type for the Es in that List, but you haven't yet said what type. You can pass a List<XYZ> to a List<?> reference, but you usually can't add elements to the List.
Plain simple List mean you aren't saying anything about its Es; it could contain anything, but you will have to cast its elements if you want to use it. This is what we used to have to write when I learnt Java1.4.2:-You should be able to see how error‑prone that technique is; it caused lots of complaints because other languages impemented generics and Java┬« didn't have them.
A List<Object> should mean that you are adding anything to it, but using all its elements as plain simple Objects. At least in theory it does. If you want to use any of its elements as a narrower type, you will have to cast it: again an error‑prone action.

[additional]Plain simple List is often called a raw type.
 
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic