Solved

Arraylist declaration

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

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

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

Suggested Solutions

Title # Comments Views Activity
Error in @AspectJ Based AOP with Spring 2 24
Desingning Refactoring existing code 2 32
CSV file parsing thru Java 13 40
JDBC performance 8 29
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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…

696 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