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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:12:52+00:00 2026-06-07T03:12:52+00:00

As far as I understand, Java Generics erases all the information concerning the parameter

  • 0

As far as I understand, Java Generics erases all the information concerning the parameter type T in the generic method (or in the generic class). That’s why we can’t use

new expression, such as

new T()

instanceof expression, such as

if(obj instanceof T)

inside a generic method.

My question is how the parameter type T works inside a generic method when it comes to casting. For example, I have 3 simple classes here:

public class People {

    String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public People(String name) {
        super();
        this.name = name;
    }

    @Override
    public String toString() {
        return "People [name=" + name + "]";
    }

}

public class GenericClass<T> {

    public T getCast(People p) {
        return (T)p;
    }
}

public class App 
{
    public static void main( String[] args )
    {
        People p = new People("Kewei");

        GenericClass<Integer> genericClass = new GenericClass<Integer>();
        Object p_object = genericClass.getCast(p);

        System.out.println(p_object);
    }
}

When it executes the (T)p in the generic method getCast(People p). Does it just take the type T as Object? or It just removes the whole (T) casting at compile time? I’ve read Bruce Eckel’s «Thinking in Java» from which I roughly understand as:

Java compiler checks at method’s entry/leave points at compile time to
make sure the internal consistency of generic method, and inserts
casting code (at compile time).

Is it the right understanding?

Thanks.

  • 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-07T03:12:55+00:00Added an answer on June 7, 2026 at 3:12 am

    You can mentally think of the compiler doing a “re-writing” of the code with generics into code without generics.

    In the generic class, T gets erased to the erasure of its upper bound, which in this case is Object, but could be something else. The cast to T becomes a cast to T’s upper bound (which in this case is Object, so it is redundant and the compiler might skip it). The method similarly returns T’s upper bound (which is Object). So there is no runtime check that the cast is valid inside the method.

    public class GenericClass {
    
        public Object getCast(People p) {
            // In this case the cast to T is removed because T's upper bound is Object.
            // But if T's upper bound was, say, Number, then there would still be a cast
            // to the upper bound. e.g. "(Number)p", and the return type would also be Number
            return p;
        }
    }
    

    In the places that call the method that returns T, the compiler will insert a cast into the actual type of T. In this case, in the main method, you have a GenericClass<Integer>, so when getCast returns T, it is cast to Integer. So in this case the runtime check now gets “moved” to the calling method, not the method with the cast.

    // somewhere else
    GenericClass genericClass = new GenericClass();
    p_object = (Integer)genericClass.getCast(p);
    

    It is also possible that the caller kept a GenericClass<S> reference (for some type parameter S in its scope), so that the result is cast to S, not a specific type. Then we would repeat the whole re-writing process again (i.e. the cast to S is re-written to a cast to its upper bound, and places that call into that method will cast the result to their actual type argument). This means that the runtime check might be deferred to yet another place, and so on.

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

Sidebar

Related Questions

As far as I understand JRuby, it is perfectly possible to use Java class
Can someone explain what were the reasons generics were introduced in Java? As far
As far as I can understand from the java.lang.Thread docs, and from other questions
Java's Executor is (as far as I understand it) an abstraction over the ThreadPool
I am trying to understand the concept of an interface in java so far
I read somewhere that java can allocate memory for objects in about 12 machine
Here is what what I understand so far: Java beans are just to help
So far I understand Httpsession concepts in Java. HttpSession ses = req.getSession(true); will create
As far as I understand from the documentation the QUdpSocket are async but, still,
As far as I understand Facebook allowes you (through the API) to post on

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.