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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:40:16+00:00 2026-06-09T05:40:16+00:00

I have been into Generics for an hour, I have certain doubts. Lets say

  • 0

I have been into Generics for an hour, I have certain doubts. Lets say I have a class like this :

class Pair<T>
{
       public T getFirst() { return first; }
}

Basically my book say this :

Type erasure: The type variables are erased and replaced by their bounding types (or
Object for variables without bounds.)

So according to my book statement the code in JVM should look like :

 class Pair
    {
           public Object getFirst() { return first; }
    }

Now if I do :

   Pair<String> pair = new Pair<>(); //I use Java 7 diamond syntax here.
   pair.getFirst()

Is that again my code need to convert from Object to String in return type of getFirst?

Now conisder :

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

The same book say’s (with respect to the above code is) :

....the files contain array of Strings.

I’m very much confused about the type erasure rule with the above example of ArrayList. In this case how come files array know that it has String array? (which is contradicting to type erasure rule)

Edit:

In order to see the resulting code of Pair(that is how type erasure have taken place), how can I use javap tool here?

Am I missing something here?

  • 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-09T05:40:17+00:00Added an answer on June 9, 2026 at 5:40 am

    Your confusion comes from what the compiler does and what the JVM does.

    Pair<String> pair = new Pair<>(); //I use Java 7 diamond syntax here.
    String first = pair.getFirst();
    

    is equivalent in the JVM to

    Pair pair = new Pair(); 
    String first = (String) pair.getFirst();
    

    In the case of Map.get()

    Map<Key, Value> map = ...
    Value value = map.get(key);
    

    is in the JVM

    Map map = ...
    Value value = (Value) map.get(key);
    

    Is that again my code need to convert from Object to String in return type of getFirst?

    No conversion occurs. The object is not altered. All that happens is there might be a cast check of the reference.

    In order to see the resulting code of Pair(that is how type erasure have taken place), how can I use javap tool here?

    Use javap -c -classpath . ClassUsingPair

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

Sidebar

Related Questions

I have been looking into coloring objects like ellipses with code such as SolidBrush
I've been developing web applications for 2 years now, and have been into simpler
I have been looking into AWS spot instances for some jobs however instead of
I have been looking into backbone.js and I can't seem to figure out how
I have been looking into CruiseControl configuration recently (I'm a complete CC noob) and
I have been looking into different systems for creating a fast cache in a
I have been looking into MVVM design patterns with WPF for a project. I
I have been looking into HTML5 manifest but I am unclear as to whether
I have been looking into packages that would enable me to have tabs in
I have been looking into integrating testing into my app based on RequireJS. I

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.