Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
How were types handled without generics in ArrayList before java version 1.5

I have a question about the history of Java.
Java has had ArrayList since 1.2.
Java has generics since version 1.5.

How was the implementation of ArrayList without generics to define the type?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

It was all done with Object (and there was a lot of casting involved), e.g.:

ArrayList list = new ArrayList();
list.add(new Thingy());
// ...
Thingy t = (Thingy)list.get(0);
// Note ---^^^^^^^^

The list only knew what it stored was Object, it was up to the code using the list to cast back to a useful type.

As you can imagine, this lead to all sorts of unpleasantness — you could put the wrong kind of object in the list, and then get ClassCastExceptions later when you tried to cast it to the type you were expecting; if you changed what was in the list, you had to change your casts everywhere and inevitably forget one, etc., etc. Generics helped remove those pain points.

about 4 years ago · Santiago Trujillo Report

0

Before generics, ArrayList have the Object as a type so that you can insert and get back the type Object which mean that you need to cast them while retrieving back to respected type. After generics these kind of redundant code got removed.

If you have close look at generic docs, your specific question been answered

Elimination of casts.

The following code snippet without generics requires casting:
List list = new ArrayList();
list.add("hello");
String s = (String) list.get(0);

When re-written to use generics, the code does not require casting:

    List<String> list = new ArrayList<String>();
    list.add("hello");
    String s = list.get(0);   // no cast
about 4 years ago · Santiago Trujillo Report

0

Actually you should not ask these type of questions in stackoverflow.What everyone expects is somecode from you to debug..But anyway you seems to be like new to stackoverflow...

You can use arraylist without generics too. For example if you use generics to hold items of particular type it looks like this..

ArrayList<String> list=new ArrayList<>();

If you don't want to use generics you can simply use this

ArrayList list =new ArrayList();

Let me tell you the very big disadvantage of using without generics.If you don't use generics then it assumes every thing as objects.So you need to type cast these to your particular datatype each time you retreive elements.

For example

for (Object o:list)
{
String s=(String)o;
System.out.println(s);
}

is what you have to do if you dont use generics.. If you use generics

then

for(String s:list)
{
System.out.println(s)
}

Hope this helps....:)

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!