wood burning stoves 2.0*
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


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: 89
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: 89
Should have used it. Thanks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
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: 1669
    
  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.
 
GeeCON Prague 2014
 
subject: can we add primitive variables in ArrayList or just objects?