Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8557095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:29:51+00:00 2026-06-11T15:29:51+00:00

What happens inside Java’s ArrayList<T> (and probably many other classes) is that there is

  • 0

What happens inside Java’s ArrayList<T> (and probably many other classes) is that there is an internal Object[] array = new Object[n];, to which T Objects are written. Whenever an element is read from it, a cast return (T) array[i]; is done. So, a cast on every single read.

I wonder why this is done. To me, it seems like they’re just doing unnecessary casts. Wouldn’t it be more logical and also slightly faster to just create a T[] array = (T[]) new Object[n]; and then just return array[i]; without cast? This is only one cast per array creation, which is usually far less than the number of reads.

Why is their method to be preferred? I fail to see why my idea isn’t strictly better?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:29:53+00:00Added an answer on June 11, 2026 at 3:29 pm

    It’s more complicated than that: generics are erased in byte code, and the erasure of T[] is Object[]. Likewise, the return value of get() becomes Object. To retain integrity of the type system, a checked cast is inserted when the class is actually used, i.e.

    Integer i = list.get(0);
    

    will be erased to

    Integer i = (Integer) list.get(0);
    

    That being the case, any type check within ArrayList is redundant. But it’s really beside the point, because both (T) and (T[]) are unchecked casts, and incur no runtime overhead.

    One could write a checked ArrayList that does:

    T[] array = Array.newInstance(tClass, n);
    

    This would prevent heap pollution, but at the price of a redundant type check (you can not suppress the synthentic cast in calling code). It would also require the caller to provide the ArrayList with the class object of the element type, which clutters its api and makes it harder to use in generic code.

    Edit: Why is generic array creation forbidden?

    One problem is that arrays are checked, while generics are unchecked. That is:

    Object[] array = new String[1];
    array[0] = 1; // throws ArrayStoreException
    
    ArrayList list = new ArrayList<String>();
    list.add(1); // causes heap pollution
    

    Therefore, the component type of an array matters. I assume this is why the designers of the Java language require us to be explicit about which component type to use.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, I'm NOT a Java noob, it just so happens that I've forgotten a
I have a arraylist(eg.CustInfo) which contains a collection of objects(Cust_details,Cust_Auth) for java classes, which
We've found a crash on a particular device that happens inside some of the
I am new to Java and I have some questions in mind regarding object
What happens when a thread is put to sleep by other thread, possible by
What happens to http requests that are being processed when you stop or restart
This happens almost every time I init a new repo. Start a new project.
I was wondering what is the standard/best implementation of that in Java NIO. This
Why Java, C and C++ (maybe other languages also) do not allow more than
Suppose I just created a package example and have two classes inside it, Main

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.