I am actually confused on the both the topics, can anyone explain me.
ArrayList<Long> queryParms = new ArrayList<Long>();
- Is the above one called generics or autoboxing and what is unboxing?
- Is it a best practice?
- Some say Autoboxing is evil thing.
- If i use generics, can i avoid autoboxing and unboxing?
The above is an example of generics. Auto-boxing would be the automatic conversion, by the compiler, of a primitive type in a wrapper type, and vice versa. In your case, for example, from a
longvariable in aLongvariable:Using generics? Yes. It allows you to specify what your list will contain. You should use them. Using auto-boxing? Yes, it simplifies the code, and you don’t have to worry about conversions between primitive variable types into wrapper (and vice-versa).
Auto-boxing isn’t evil (IMHO). They are some corner cases in which auto-boxing can be very annoying, but if you know how it works, you shouldn’t have to worry about it. Here is the Sun (now Oracle) paper on auto-boxing, if you need to know more about it.
If you want to create a list that contains wrappers (in your case,
Long), you’ll have to deal with type conversion. You can use explicit type conversion, or you can use auto-boxing.