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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:31:13+00:00 2026-06-05T18:31:13+00:00

Given this code: List<Integer> ints = new ArrayList<Integer>(); // Type mismatch: // cannot convert

  • 0

Given this code:

List<Integer> ints = new ArrayList<Integer>();

// Type mismatch: 
// cannot convert from Class<capture#1-of ? extends List> to Class<List<Integer>>       
Class<List<Integer>> typeTry1 = ints.getClass(); 

// Type safety: 
// Unchecked cast from Class<capture#2-of ? extends List> to Class<List<Integer>>
Class<List<Integer>> typeTry2 = (Class<List<Integer>>) ints.getClass();

// List is a raw type. References to generic type List<E> should be parameterized
Class<? extends List> typeTry3 = ints.getClass(); 

Is there a way to get the Class of a List<Integer> without an error or warning? I can suppress the warnings easily enough, but if Java requires me to suppress a warning for this valid code, I am very disappoint.

On the other hand, if warning suppression is the only way, what is the safest to suppress?

  • 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-05T18:31:14+00:00Added an answer on June 5, 2026 at 6:31 pm

    This is a real Catch-22 in Java.

    The compiler warns you if you don’t add a generic type to List:

    // WARN: List is not typed
    Class<? extends List> typeTry3 = ints.getClass();
    

    That’s because, in most cases, it’s really best to type your Lists.

    However, because of type erasure, there’s no way for Java to figure out the generic type of List at runtime, and the compiler knows that. Therefore, there is no method on Class that will returned a typed object:

    // ERROR: ints.getClass() doesn't return a Class<List<Integer>>, it returns a Class<List>
    Class<? extends List<? extends Integer>> var = ints.getClass();
    

    So you must cast it to a typed list. However, as you know, since there is no runtime type checking, Java warns you about any casts to a typed variable:

    // WARN: Casting to typed List
    Class<List<Integer>> typeTry2 = (Class<List<Integer>>) ints.getClass();
    

    Any attempt to get around this is essentially a means of confusing the compiler, and will inevitably be convoluted.

    Your best bet then is to go with Option B:

    On the other hand, if warning suppression is the only way, what is the safest to suppress?

    The safest way to suppress this warning is to make your @SuppressWarnings("unchecked") annotation as localized as possible. Put them above each individual unchecked cast. That way it’s absolutely clear who’s causing the warning.

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

Sidebar

Related Questions

Given this code: List<string> things = new List<string>(); foreach (string thing in things) {
Given this piece of code : public static void writeFile(File file,List buffer)throws IOException{ File
This portion of code checks if a number given (First element in a list)
Given this code: var arrayStrings = new string[1000]; Parallel.ForEach<string>(arrayStrings, someString => { DoSomething(someString); });
Given this sample code: #include <iostream> #include <stdexcept> class my_exception_t : std::exception { public:
Question: Given a list of integers, write python code to create a new list
So I was given this code in a folder of bits and pieces that
I was given this Python code that would calculate an MD5 value for any
Can anyone explain why this code with the given routes returns the first route?
This code works great for generating thumbnails, but when given a very large (100MB+)

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.