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

  • Home
  • SEARCH
  • 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 6738467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:22:20+00:00 2026-05-26T11:22:20+00:00

I wonder why Java compiler doesn’t trust in this line of code : List<Car>

  • 0

I wonder why Java compiler doesn’t trust in this line of code :

List<Car> l = new ArrayList();

and expects to have a typed ArrayList :

List<Car> l = new ArrayList<Car>();

Indeed, compiler indicates an unchecked assignment with the first case.

Why doesn’t compiler see that this ArrayList() has just been created and so it’s impossible to find already in it some objects other than ‘Car’?

This warning would make sense if the untyped ArrayList was created before but doesn’t in this case …

Indeed, since List is typed as ‘Car’, all futures “l.add(‘object’)” will be allowed only if ‘object’ is a ‘Car’. => So, according to me, no surprise could happen.

Am I wrong ?

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-05-26T11:22:20+00:00Added an answer on May 26, 2026 at 11:22 am

    Why doesn’t compiler see that this ArrayList() has just been created and so it’s impossible to find already in it some objects other than ‘Car’?

    The simple answer is “Because it is not allowed to.”

    The compiler has to implement the Java Language Specification. If some compiler writer goes and adds a bunch of smarts to the compiler to allow things that “everyone” knows are safe, then what he’s actually done is to introduce a portability problem. Code compiled and tested with this compiler will give compilation errors when compiled with a dumb (or more accurately, strictly conformant) Java compiler.

    So why doesn’t the JLS allow this? I can think of a couple of possible explanations:

    • The JLS writers didn’t have time to add this to the specification; e.g. to consider all of the ramifications on the rest of the specification.
    • The JLS writers couldn’t figure out a sound way to express this in the specification.
    • The JLS writers didn’t want to put something into the specification that they weren’t sure was implementable in a real world compiler … without being too burdensome on the compiler writer.

    And then there is the associated question of whether a compiler could implement such a check. I’m not qualified to answer that … but I know enough about the problem to realize that it is not necessarily as simple to solve as one might imagine.

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

Sidebar

Related Questions

I wonder if this is possible with ant and java : I have a
I wonder why smalltalk doesn't make use of java-style inner class. This mechanism effectively
I'm having a headache implementing this (awful) pseudo-java code (I wonder: why the hell
Consider this code: class Outer { class Inner } In Java it would be
I have following Java code: String s2 = SUM 12 32 42; Pattern pat1
I wonder if anyone can advise on a Java webapp question? I have a
I don't have any knowledge of Java platform and I wonder which tools (and
I wonder if it possible to not have jython automagicaly transform java objects to
I wonder is there any tool (preferably free) that can generate Java source code
After readings How many characters can a Java String have? I started wonder wonder:

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.