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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:14:26+00:00 2026-05-12T07:14:26+00:00

public void wahey(List<Object> list) {} wahey(new LinkedList<Number>()); The call to the method will not

  • 0
public void wahey(List<Object> list) {}

wahey(new LinkedList<Number>());

The call to the method will not type-check. I can’t even cast the parameter as follows:

wahey((List<Object>) new LinkedList<Number>());

From my research, I have gathered that the reason for not allowing this is type-safety. If we were allowed to do the above, then we could have the following:

List<Double> ld;
wahey(ld);

Inside the method wahey, we could add some Strings to the input list (as the parameter maintains a List<Object> reference). Now, after the method call, ld refers to a list with a type List<Double>, but the actual list contains some String objects!

This seems different to the normal way Java works without generics. For instance:

Object o;
Double d;
String s;

o = s;
d = (Double) o;

What we are doing here is essentially the same thing, except this will pass compile-time checks and only fail at run-time. The version with Lists won’t compile.

This leads me to believe this is purely a design decision with regards to the type restrictions on generics. I was hoping to get some comments on this decision?

  • 1 1 Answer
  • 3 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-12T07:14:27+00:00Added an answer on May 12, 2026 at 7:14 am

    What you are doing in the “without generics” example is a cast, which makes it clear that you are doing something type-unsafe. The equivalent with generics would be:

    Object o;
    List<Double> d;
    String s;
    
    o = s;
    d.add((Double) o);
    

    Which behaves the same way (compiles, but fails at runtime). The reason for not allowing the behavior you’re asking about is because it would allow implicit type-unsafe actions, which are much harder to notice in code. For example:

    public void Foo(List<Object> list, Object obj) {
      list.add(obj);
    }
    

    This looks perfectly fine and type-safe until you call it like this:

    List<Double> list_d;
    String s;
    
    Foo(list_d, s);
    

    Which also looks type-safe, because you as the caller don’t necessarily know what Foo is going to do with its parameters.

    So in that case you have two seemingly type-safe bits of code, which together end up being type-unsafe. That’s bad, because it’s hidden and therefore hard to avoid and harder to debug.

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

Sidebar

Related Questions

public void Foo (IEnumerable<object> objects) { } var strings = new List<string>{first, second, third};
public void DoRiskyThings(List<Action> tasks) { List<Exception> exceptions = new List<Exception>(); foreach(Action task in tasks)
public void Save(object s, EventArgs e) { HtmlMeta meta = new HtmlMeta() { Name
public void onBackPressed() { Intent setIntent = new Intent(this,xxxxx.class); startActivity(setIntent); return; } I call
public void run() { try { ois = new ObjectInputStream(clientSocket.getInputStream()); Object o; while ((o
public void test() { List<int> list = new List<int>(); list.Add(1); list.Add(2); list.Add(3); for (int
public void sort() { datelist = new List<DateTime>(rdate); //timedates intlist = new List <Int>(rint);
public void Save() { XmlSerializer Serializer = new XmlSerializer(typeof(DatabaseInformation)); /* A first chance exception
public void DoPing(object state) { string host = state as string; m_lastPingResult = false;
public void openReport() { try { HashMap params = new HashMap(); params.put(aapor, 19); JasperReport

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.