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

The Archive Base Latest Questions

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

Hello I have a method that should return different results depending on the type.

  • 0

Hello I have a method that should return different results depending on the type.

I can do like this to check the generic type.

public <T> T search(final String query){
 T returnValue = null;
 if (returnValue instanceof String){ }
 if (returnValue instanceof Integer){ }
 if (returnValue instanceof MyObject){ }

But why can’t I do like this?

public <T> T search(final String query){
 T returnValue = null;
 if (T instanceof String){ }
 if (T instanceof Integer){ }
 if (T instanceof MyObject){ }

Calling code.

String id = myObjcet.<String> search("select ...");
  • 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-25T11:46:09+00:00Added an answer on May 25, 2026 at 11:46 am

    Java implements generics “by erasure”. That means that at runtime there is (nearly) no knowledge of generics at all. Instead, the compiler checks for generics, but under the cover it converts everything to Object (or to the nearest generics boundary).

    If you decompile that method using javap, you’ll see that it reads :

    public Object search(...
    

    Every reference to T is removed.

    This is a serious limitation of generics in Java, which is reflected often also in the JRE classes with constructs like :

    SomeClass<String> x = new SomeClass<String>(String.class);
    

    Where the third repetition of String (String.class) is needed because the constructor needs to know which class it is operating on.

    This paradigm, despite being horrible, could solve your problem.

    Informations about generics are actually contained in the .class files, as metadata, they can be inspected using reflection and up to some extent also resolved. However, this is not possible in a case like yours, but it is for example used by ORMs on list getters. for example, using Hibernate, a getter like this :

    public List<Group> getGroups() {
    

    Can be inspected by Hibernate so that it knows that the list is supposed to hold instances of Group, adapting its queries accordingly.

    (However, even if generics where not implemented this way, T is not an instance, it is a class, writing String instanceof String is equally wrong. It should be if (T.equals(String.class)) )

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

Sidebar

Related Questions

Hello I have this list that i am working on. When i move the
So I have a a text file that looks something like this: public >ret
I have a marshalable class that contains a factory method. The factory method can
Hello I have been having trouble with this for a while now. I have
Hello i have this form filling javascript: function onLine(code,nn) { document.writeform.bericht.value+=code; document.writeform.bericht.focus(); document.writeform.nickname.value+=nn; write1();
I have a method that accepts a bunch of strings in seperate parameters public
I have an ImageView that you can use to do a one-finger pan, or
Let's say I have: <input type=text id=first attribute1=test attribute2=hello attribute3=happy> <input type=text id=first attribute1=test
i have a requirement to build some sort of services that can easily be
Suppose I have a Java class like this: public class Test { static {

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.