• Post Reply Bookmark Topic Watch Topic
  • New Topic

How does primitive datatype get added to a collection?  RSS feed

 
Davie Lin
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it

1. added as it is

or

2. wrap into a corresponding wrapper class and added into collection?

Thank you
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Collections do not work on primitives so they are wrapped using autoboxing into Integer, Long etc objects.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Collections do not work on primitives so they are wrapped using autoboxing into Integer, Long etc objects.
At least since September 2004 when Java 5 was issued. Before that you had to use new Integer(123) or similar.
 
Davie Lin
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This was a question of a Java assessment test. I wasn't sure but luckily I picked 2 as my right answer. I figure it had to be 2 or else declaring the collection<type> would be meaningless
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!