You are getting the warning because ArrayList
is part of java generics. Essentially, it's a way to catch your type errors at compile time. For example, if you declare your array list with types Integer (ArrrayList<Integer>
) and then try to add Strings to it, you'll get an error at compile time - avoiding nasty crashes at runtime.
The first syntax is there for backward compatibility and should be avoided whenever possible (note that generics were not there in older versions of java).
Second and third examples are pretty much equivalent. As you need to pass an object and not a primitive type to add
method, your 3
is internally converted to Integer(3)
. By writing a string in double-quotes you effectively are creating a String
object. When calling String("ss")
you are creating a new String
object with value being the same as the parameter ("ss").
Unless you really do need to store different types in your List, I would suggest actually using a proper type declaration, e.g. ArrayList<Integer> = new ArrayList<Integer>()
- it'll save you a lot of headache in the long run.
If you do need multiple datatypes in the list, then the second example is better.