• 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

Difference between declaring ArrayList the following way

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello folks,

What is the diiference between declaring an arrayList between the following two ways:

ArrayList aAl1 = new ArrayList();

or

ArrayList<String> aAl2= new ArrayList<>();

In the aAL2, we can only store String objects. In aAL1 , we can store any kind of primitive data types or user defined classes.

Is that correct?
thanks
nath
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Technically, you can store anyhing inside ArrayList<String>.
However, this requires unchecked calls and is strongly discouraged. The compiler will give you a warning.This prints:
[Hello, 1, java.lang.Object@12a3a380]

You can not store primitive values in an ArrayList. In the example above (line 6) there is the autoboxing happening and what really is stored is an instance of Integer.
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Allowing primitive generic types is being worked on and will be available in a future release of Java.

The difference between the two declarations is that the compiler can make checks when putting things in the arraylist, and has confidence of what type will come out of the arraylist.

As shown you can get around these checks if you want, but that is something that would be discouraged.

Edit: fixed some auto corrections my phone made
 
narendra nath
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks both of you.

 
Marshal
Posts: 76872
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would say the correct way to declare and instantiate a List is like this:-
List<Foo> myList = new ArrayList<Foo>();
That is called programming to the interface: you can use every method in the List interface and can change the implementation to LinkedList or similar very simply. In Java7 you have type inference
List<Foo> myList = new ArrayList<>();
You can find more about the <> in the Java™ Tutorials.
 
A wop bop a lu bob a womp bam boom. Tutti frutti ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic