Solved

Arraylist declaration

Posted on 2013-11-25
2
311 Views
Last Modified: 2013-11-26
Hi,

What are the advantages of declaring ArrayList as below as of J2SE5.0

ArrayList<Student> students = new ArrayList<Student>();
vs as below as J2SE 1.4
ArrayList students = new ArrayList();

please advise
Any links resources ideas highly appreciated. Thanks in advance
0
Comment
Question by:gudii9
2 Comments
 
LVL 26

Accepted Solution

by:
dpearson earned 300 total points
ID: 39676157
The first form makes it clear that the Objects in the list are Students.

The second form - you just have a list of Objects - anything could be in there (Students, Integers, etc.).

List<Student> newStudents = new ArrayList<Student>() ;
List oldStudents = new ArrayList() ;

Student student1 = newStudents.get(0) ; // Note: no cast in Java 5
Student student2 = (Student)oldStudents.get(0) ; // Have to cast to get the value
Integer student3 = (Integer)oldStudets.get(0) ; // Whoops - cast to wrong type - won't get an error until I run this code

Doug
0
 
LVL 12

Assisted Solution

by:Sharon Seth
Sharon Seth earned 200 total points
ID: 39676788
Some more points on this:

This feature is called Generics . It is for compile time type checking  , which would otherwise give you a nasty class cast exception during run time . As Doug put in his student sample , with generics , the compiler always makes sure that you only use that list for students .

Note that this has been implemented only in the compiler and not in the JVM . JVM does not know/recognise generics .
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bunnyEars challenge 6 62
array220 challenge 8 45
wordlen challenge 3 46
best (free) software to access postgres db (java) 1 18
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now