[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Generics and Autoboxing

Posted on 2006-04-07
3
Medium Priority
?
137 Views
Last Modified: 2010-03-31
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;
0
Comment
Question by:jedistar
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 total points
ID: 16402288
>>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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16402300
>>have to cast

should read

having to call new Integer(n)
0
 
LVL 92

Expert Comment

by:objects
ID: 16404540
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();
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month18 days, 23 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question