• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ArrayList

 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two initializations of ArrayLists


My questions are a. In the first initialization i know that the ArrayList is a String type, but what about the type of second ArrayList ??
b. Can an ArrayList hold elements of different primitive types ??
 
Ranch Hand
Posts: 89
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shivang sarawagi wrote:There are two initializations of ArrayLists


My questions are a. In the first initialization i know that the ArrayList is a String type, but what about the type of second ArrayList ??
b. Can an ArrayList hold elements of different primitive types ??



a. you are pertaining of the second arraylist as arraylist of the second line? then ArrayList is also equal to ArrayList<Object>

b. ArrayList without generics can hold primitive types but autoboxing will occur first. (Eg. int primitive will be automatically be converted to Integer, since Integer extends Number extends Object, it is possible. )
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alexander Sales wrote: . . .

b. ArrayList without generics can hold primitive types but autoboxing will occur first. . . .

Afraid that is not correct. A List cannot hold primitives, but you can add what appears to be a primitive by auto-boxing.The real difference is that you will most probably have to cast the elements from the second List (without the <>) and casting is error-prone.
 
Ranch Hand
Posts: 75
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

a. In the first initialization i know that the ArrayList is a String type, but what about the type of second ArrayList ??



Even if you declared the first list as ArrayList<String>, it actually holds Objects and not necessarily Strings. The difference between the two declarations is that in the first one, the compiler is aware that the list should contain Strings.
For example, run this and check out the output:

 
Do not meddle in the affairs of dragons - for you are crunchy and good with ketchup. Crunchy tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic