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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:32:06+00:00 2026-06-10T14:32:06+00:00

When writing a generic method to process data for a form, I came across

  • 0

When writing a generic method to process data for a form, I came across with the following (as I see it) unexpedted behavior. Given the following code:

public class Test {
   public <T> void someGenericMethod(Integer a) {
      @SuppressWarnings("unchecked")
      T t = (T) a;
      System.out.println(t);
      System.out.println(t.getClass());
   }

   public static void main(String[] args) {
      Test test = new Test();
      test.<BigDecimal>someGenericMethod(42);
   }  
}

AFAIK, the code above should generate a ClassCastException in the line T t = (T) a because the method call in main is setting the parametrized type to BigDecimal and casting from Integer to BigDecimal is not allowed, conversely to what I expected, the program executed well and printed the following:

42
class java.lang.Integer 

In fact, if I add another parameter to the method signature (like String b) and make another assigment T t2 = (T) b, the program prints

42
class java.lang.String 

Why the t variable changed it’s type to Integer (is, by any chance, making some kind of promotion on the type T to Object)?

Any explanation on this behavior is welcome

  • 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-10T14:32:08+00:00Added an answer on June 10, 2026 at 2:32 pm

    (T) a is an unchecked cast: due to type erasure, the runtime has no way of knowing what type T is, so it can’t actually check if a belongs to type T.

    The compiler issues a warning when you do this; in your case, you’ve suppressed that warning by writing @SuppressWarnings("unchecked").


    Edited to add (in response to a further question in the comments below):

    If you want to check the cast, you can write this:

    public class Test {
       public <T> void someGenericMethod(Class<T> clazz, Integer a) {
          T t = clazz.cast(a);
          System.out.println(t);
          System.out.println(t.getClass());
       }
    
       public static void main(String[] args) {
          Test test = new Test();
          // gives a ClassCastException at runtime:
          test.someGenericMethod(BigDecimal.class, 42);
       }
    }
    

    by passing in clazz, you allow the runtime to check the cast; and, what’s more, you allow the compiler to infer T from the method arguments, so you don’t have to write test.<BigDecimal>someGenericMethod anymore.

    Of course, the code that calls the method can still circumvent this by using an unchecked cast:

    public static void main(String[] args) {
       Test test = new Test();
       Class clazz = Object.class;
       test.someGenericMethod((Class<BigDecimal>) clazz, 42);
    }
    

    but then that’s main‘s fault, not someGenericMethod‘s. 🙂

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

Sidebar

Related Questions

I'm having some trouble with a generic method I'm writing. It has the following
I'm currently in the process of writing a little blog / generic posting system
I've seen the myriad threads sprawled across the Internet about the following similar code
I'm having a problem writing a generic method to retrieve AD Groups or Users
I usually want to keep my code as generic as possible. I'm currently writing
I am writing a method that will take a screenshot of a passed form
I am writing a generic extension method for IEnumerable for mapping a list of
I'm writing a C++ project and have a generic evaluate method in a class
I'm trying to writing a generic method that will load a record of a
I was writing a generic class to read RSS feed from various source and

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.