?
Solved

Arraylist declaration

Posted on 2013-11-25
2
Medium Priority
?
335 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 28

Accepted Solution

by:
dpearson earned 1200 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 800 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

649 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