Code 1:
ArrayList arr = new ArrayList();
arr.add(3);
arr.add("ss");
Code 2:
ArrayList<Object> arr = new ArrayList<Object>();
arr.add(3);
arr.add("ss");
Code 3:
ArrayList<Object> arr = new ArrayList<Object>();
arr.add(new Integer(3));
arr.add(new String("ss"));
all the above three codes are working fine.. can some one tell me the which is prefered and why.. and why the eclipse compiler always gives warning when type of arguments are not mentioned to the Arraylist.. thanks in advance..
First simple rule: never use the
String(String)
constructor, it is absolutely useless (*).So
arr.add("ss")
is just fine.With
3
it's slightly different:3
is anint
literal, which is not an object. Only objects can be put into aList
. So theint
will need to be converted into anInteger
object. In most cases that will be done automagically for you (that process is called autoboxing). It effectively does the same thing asInteger.valueOf(3)
which can (and will) avoid creating a newInteger
instance in some cases.So actually writing
arr.add(3)
is usually a better idea than usingarr.add(new Integer(3))
, because it can avoid creating a newInteger
object and instead reuse and existing one.Disclaimer: I am focusing on the difference between the second and third code blocks here and pretty much ignoring the generics part. For more information on the generics, please check out the other answers.
(*) there are some obscure corner cases where it is useful, but once you approach those you'll know never to take absolute statements as absolutes ;-)
The second form is preferred:
Always specify generic arguments when using generic types (such as
ArrayList<T>
). This rules out the first form.As to the last form, it is more verbose and does extra work for no benefit.
Well by doing the above you open yourself to run time errors, unless you are happy to accept that your arraylists can contains both strings and integers and elephants.
Eclipse returns an error because it does not want you to be unaware of the fact that by specifying no type for the generic parameter you are opening yourself up for run time errors. At least with the other two examples you know that you can have objects in your Arraylist and since Inetegers and Strings are both objects Eclipse doesn't warn you.
Either code 2 or 3 are ok. But if you know you will have either only ints or only strings in your arraylist then I would do
or
respectively.
Two last variants are the same,
int
is wrapped toInteger
automatically where you need anObject
. If you not write any class in <> it will beObject
by default. So there is no difference, but it will be better to understanding if you writeObject
.The second one would be preferred:
However, having two different types of object in the same list has a bit of a bad design smell. We need more context to speak on that.
Actually, a third is preferred:
This avoids autoboxing (
Integer.valueOf(3)
versus3
) and doesn't create an unnecessary String object.Eclipse complains when you don't use type arguments with a generic type like ArrayList, because you are using something called a raw type, which is discouraged. If a class is generic (that is, it has type parameters), then you should always use type arguments with that class.
Autoboxing, on the other hand, is a personal preference. Some people are okay with it, and some not. I don't like it, and I turn on the warning for autoboxing/autounboxing.