This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes can we add primitive variables in ArrayList or just  objects? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "can we add primitive variables in ArrayList or just  objects?" Watch "can we add primitive variables in ArrayList or just  objects?" New topic
Author

can we add primitive variables in ArrayList or just objects?

Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

can we add primitive variables in arraylist or just objects


http://www.lifesbizzare.blogspot.com || OCJP:81%
Chinna Eranna
Ranch Hand

Joined: Dec 08, 2009
Posts: 174

You can add *only* objects.

You can use.. wrapper classes.. for store the primitive data.


- Chinna
Karthick Dharani Vidhya
Ranch Hand

Joined: Feb 23, 2008
Posts: 88
With the AUTO BOXING feature, the below code will work fine



But even though you add primitive ones here, Auto boxing is done and all these values are added as Integer objects


Karthick D V
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

Karthick Dharani Vidhya wrote:With the AUTO BOXING feature, the below code will work fine



But even though you add primitive ones here, Auto boxing is done and all these values are added as Integer objects


why aint you used

Karthick Dharani Vidhya
Ranch Hand

Joined: Feb 23, 2008
Posts: 88
Should have used it. Thanks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38036
    
  22
Vishal Hegde wrote:why aint you used

Surely you mean this?
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

Ritchie sir , arent both correct?

i mean

ArrayList<Integer>a=new ArrayList<Integer>(); //as arraylist is a class

List<Integer>iList=new ArrayList<Integer>(); //list is an interface
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1659
    
  11

Vishal Hegde wrote:Ritchie sir , arent both correct?

i mean

ArrayList<Integer>a=new ArrayList<Integer>(); //as arraylist is a class

List<Integer>iList=new ArrayList<Integer>(); //list is an interface


Campbell's solution is best. It is my understanding that you should always code to an interface (when possible) and not its implementation.

I think this allows for greater flexibility if you need to change something down the line.

When you do things right, people won't be sure you've done anything at all.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: can we add primitive variables in ArrayList or just objects?
 
Similar Threads
ArrayList / Vector
Array List
How do we decide on when to create primitive variables and when to create objects?
what is dereference?
help