When you use generics you will have to include a reference to a type of object that is going in the List. In this case you are trying to tell the ArrayList to reference only values of type long, or int or double, etc...
You need to declare the objects that will be referenced in a generic by usinig big D Double and Big I Integer and Big L Long as these are object types.
I'm not too sure if you can box a Long as an object type but check your API and you will see.
This code is *not* the same as yours... but let's back up a bit.
The collection classes are designed to store objects -- not primative values. So declaring a ArrayList<long> is not valid. What this declaration does is declare an array list of Long -- the java.lang.Long class to be exact. This is an object type and is valid.
And the reason this will work is because of another feature of Java -- autoboxing. When you add() a primative long, the compiler will figure out that there is no such add() method, that takes a long, and automatically create a java.lang.Long object for you.
Originally posted by jacob rich: I thought arraylist were restricted to being coded as arraylist<object> since they fall under the Object superclass in the inheritance tree...
Welcome to JavaRanch!
Java has a "single-rooted hierarchy," meaning that every class is a subclass of Object.
What you're probably recalling is that references in Java collections (like a List) are automatically upcast to type Object. Prior to the introduction of generics with Java 1.5, there was no type checking when adding references to a collection -- it was left to the programmer to ensure that only the proper types were added. And these references needed be to explicitly downcast when removed from a collection. With generics, a specific type can be associated with the collection, allowing the compiler to provide type checking and implicit downcasting.
But note that Java is case sensitive, so arraylist is not the same as ArrayList, object is not the same as Object, and so on.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Joined: Jul 14, 2006
Ok so ArrayList can only store objects, so by declaring ArrayList<long> is not valid as long in lower case is a primitive value. But by declaring it as ArrayList<Long> with Long as upper case it tells the ArrayList to expect Objects of type long so then you could declare a primitive long variable and add that primitive long variable to the ArrayList casted to Object Long.
If you add a long (primitive) to a List<Long>, there are two things happening: First, the primitive value will be boxed as a wrapper type Long, and then the reference to that new Long object will be upcast to type Object for storage in the List.