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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:00:28+00:00 2026-06-07T10:00:28+00:00

Object stringMap = new HashMap<String, String>(){{ put(1, a); }}; Map<Integer, String> integerMap = (Map<Integer,

  • 0
Object stringMap = new HashMap<String, String>(){{ put("1", "a"); }};
Map<Integer, String> integerMap = (Map<Integer, String>)stringMap; // Why doesn't Java throw an exception at run-time?

// I know this is not a problem if stringMap is declared as Map<String, String>.
// However, the actual code above was using Spring Bean.
// Map<Integer, String> integerMap = (Map<Integer, String>)context.getBean("map");

System.out.println(integerMap.get(1)); // prints null
System.out.println(integerMap.get("1")); // prints a

Q1. Why Java allows such casting at run-time?

Q2. If using bean, what is the best practice to avoid this error?

  • 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-07T10:00:30+00:00Added an answer on June 7, 2026 at 10:00 am

    Q1. Because at run-time, all generic info is already stripped away, so the two Map types are indistinguishable for the run-time environment. The generics are only there to help the compiler enforce type safety. To quote the Java Tutorials:

    Generics are implemented by type erasure: generic type information is
    present only at compile time, after which it is erased by the
    compiler. The main advantage of this approach is that it provides
    total interoperability between generic code and legacy code that uses
    non-parameterized types (which are technically known as raw types).
    The main disadvantages are that parameter type information is not
    available at run time, and that automatically generated casts may fail
    when interoperating with ill-behaved legacy code.

    Q2. Don’t use raw-typed Maps. If you have to, be really really careful when you typecast them.

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

Sidebar

Related Questions

Object ele=a.get(i); if(ele instanceof java.lang.Integer){//cast to integer: print(found Int); }else{ //cast to string: print(found:
object[] objs = new object[]{one,two,three}; Are the strings stored in the array as references
object[] objArray = new object[]{blah, 4, whatever}; foreach(var value in objArray) vs. foreach(object value
Object doesn't support this property or method It's this line. pthumb = $(#pthumb).attr(src); Does
object(stdClass)[1] public 'inbox' => array 0 => object(stdClass)[2] public 'from' => string '55512351' (length=8)
object ReassignTest extends App { class X(var i : Int) def x = new
object nullobject = null; object myobject = new myobject(); console.writeline(nullobject=+nullobject+myobject=+myobject);
object Test extends Application { // compiles: Map[Int, Value]( 0 -> KnownType(classOf[Object]), 1 ->
object obj = Hello; string str1 = (string)obj; string str2 = obj.ToString(); What is
The debugger is giving me 'bad ptr' when I create a new string array

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.