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 5840615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:46:24+00:00 2026-05-22T11:46:24+00:00

I am wondering if there is any difference in runtime between a generic container

  • 0

I am wondering if there is any difference in runtime between a generic container implemented using the language features for generics, introduced Java 1.5, compared to doing it with just inheritance and explicit typecasts. And if there is, which would give the highest performance.

To be specific, lets say I have a base class and a subclass:

public class Base { }
public class Sub extends Base { }

And I define a container type in terms of the base class. Then what I am interested in is what happens when a container instance is known to contain a certain subclass. Before 1.5 I would have no choice but to implement the container like this (never mind that it is too simplified to make actual sense):

public class OldstyleContainer {
    private Base x;

    public void set(Base x) { this.x = x; }
    public Base get() { return x; }
}

And a use of the class where the specific type of the known element is known could look like this:

public Sub oldstylePut(OldstyleContainer c, Sub s) {
    Sub t = (Sub) c.get();
    c.set(s);
    return t;
}

Now, with the language features for generics, I would instead define the container like this:

public class GenericsContainer<T extends Base> {
    private T x;

    public void set(T x) { this.x = x; }
    public T get() { return x; }
}

And the corresponding use would be like this:

public Sub genericsPut(GenericsContainer<Sub> c, Sub s) {
    Sub t = c.get();
    c.set(s);
    return t;
}

The generic version code looks (very) slightly simpler, because there is no need for an explicit cast. But my question is, is there any real difference in runtime, or does that cast still exist in the byte code? Is there any other difference?

  • 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-05-22T11:46:25+00:00Added an answer on May 22, 2026 at 11:46 am

    Generics are erased – so by the time the code runs the compiler has put the casts in anyway. You just don’t have to do them yourself in the source code when using Generics. So don’t consider performance – use Generics to make code safer.

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

Sidebar

Related Questions

I was just wondering if there is any difference between the two different new
I was wondering, what (if any) is the difference between creating objects using: NSThing
I'm wondering if there is any difference between div.class and .class as CSS selectors
I was wondering if there is any difference in performance/memory use between movieclip symbol
I am wondering whether there is any difference between inlining functions on a linker
Hi I was wondering if there is any difference between initializing object like this
So I was wondering if there are any major differences between the various implementations
I am wondering if there is any performance differences between String s = someObject.toString();
Possible Duplicates: Is there any difference between “string” and 'string' in Python? Single quotes
Apart from an architectural point of view, i'm wondering if there is any difference

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.