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

  • SEARCH
  • Home
  • 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 7791309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:50:48+00:00 2026-06-01T21:50:48+00:00

I was reading a discussion about C++ templates and C# generics, and how they

  • 0

I was reading a discussion about C++ templates and C# generics, and how they are different from Java’s type-erased generics. I read a statement that said that Java still uses casting at runtime, for instance when dealing with collections. If this is true, I wasn’t aware of it!

Let’s say that I have code such as:

ArrayList<SomeClass> list = new ArrayList<SomeClass>();
...
SomeClass object = list.get(0);

My question is. Is this effectively compiled to

ArrayList list = new ArrayList();
...
SomeClass object = (SomeClass) list.get(0);

If so, why? I thought that the fact that list is of type ArrayList<SomeClass> guarantees, at compile time and run time, that only SomeClass will be stored inside the ArrayList? Or can you ever do unsafe type-casting to transform an ArrayList<OtherClass> into an ArrayList<SomeClass>?

Are there other occasions where at-runtime type casting is done in Java generics?

Finally, if casting at run time is indeed used, are there occasions when the JIT can elide the run time cast check?

(Please refrain from answering/commenting that micro-optimisations are not worth it, preemptive optimisation is root of all evil, etc. I see these on other similar questions. These points are well understood, but they do not take away the point of trying to understand how type-erased generics are implemented under the hood.)

  • 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-01T21:50:51+00:00Added an answer on June 1, 2026 at 9:50 pm

    Here is a short program I wrote:

    public class Test<T> {
    
      public T contents;
    
      public Test(T item) {
        contents = item;
      }
    
      public static void main(String[] args) {
    
        Test<String> t = new Test<String>("hello");
        System.out.println(t.contents);
    
      }
    
    }
    

    Try compiling it with javac, and then look at the bytecode with javap -verbose. I’ve selected a few interesting lines:

    public java.lang.Object contents;
    

    This should turn up just before the definition of the Test constructor. In the example code it was of type T, now it is an Object. That’s erasure.

    Now, looking at the main method:

    public static void main(java.lang.String[]);
    Code:
       Stack=3, Locals=2, Args_size=1
       0:   new #3; //class Test
       3:   dup
       4:   ldc #4; //String hello
       6:   invokespecial   #5; //Method "<init>":(Ljava/lang/Object;)V
       9:   astore_1
       10:  getstatic   #6; //Field java/lang/System.out:Ljava/io/PrintStream;
       13:  aload_1
       14:  getfield    #2; //Field contents:Ljava/lang/Object;
       17:  checkcast   #7; //class java/lang/String
       20:  invokevirtual   #8; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
       23:  return
    

    Then we can see the checkcast command at line 17, just before the println – this is where Java casts from Object to the erased generic type – String

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

Sidebar

Related Questions

I was reading about Clojure and found a discussion about Java not supporting tail
I am reading Effective Java . In the discussion about finalize, he says C++
I wanted to start a discussion about the details that you cover in your
I'm looking at solutions to a problem that involves reading keyed data from more
After reading this discussion and this discussion about using CrashRpt to generate a crash
Reading this discussion , I am wondering about the pro/cons of SecureString vs hash
I have been reading a lot about Haskell lately, and the benefits that it
I was recently reading this discussion at SO where somebody commented that not all
I was reading the article Java theory and practice: Building a better HashMap that
Reading about the Dispose pattern , I see the documentation repeatedly refer to cleaning

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.