Generics and Autoboxing

1. For generics, does it mean we define the <Type> during declaration so it makes our life easier?
i.e List<String> ls = new ArrayList<String>;

so the above, the list can only contain String objects and no other types?
this <type> can only be used on collections and arrays?

1a. What is the use of Collection<object> and Collection<?>

2. I'm having issues understanding Autoboxing. I read http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html, still aint sure what its doing and what its for? Is it trying to say Autoboxing is automatic now.. and it is no problem if we do:

int x = 5;
Integer y = x;
int z = y;
jedistarAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
>>1. For generics, does it mean we define the <Type> during declaration so it makes our life easier?

Not just to make life easier - it provides compile time type checking

>>so the above, the list can only contain String objects and no other types?

Yes

>>still aint sure what its doing and what its for?

It's to save you have to cast and call intValue() and the like

>>Is it trying to say Autoboxing is automatic now.. and it is no problem if we do:

It's automatic

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
>>have to cast

should read

having to call new Integer(n)
Mick BarryJava DeveloperCommented:
1. Allows molre errors to be picked up at compile time, rather than at runtime
Also saves you lots of casting because the compiler knows what type is in collection

1a. Have a read of the following for a good explanation of generics

http://mindprod.com/jgloss/generics.html

2. without it you'd need:

int x = 5;
Integer y = new Integer(x);
int z = y.intValue();
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.