Solved

Arraylist declaration

Posted on 2013-11-25
2
318 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

816 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

8 Experts available now in Live!

Get 1:1 Help Now