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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:50:03+00:00 2026-05-23T22:50:03+00:00

Why do we lose type safety when using List and not while using List<Object>

  • 0

Why do we lose type safety when using List and not while using List<Object>? Aren’t they basically the same thing?

EDIT: I found that the following gives a compilation error

public class TestClass
{
    static void func(List<Object> o, Object s){
        o.add(s);
    }

    public static void main(String[] args){
        func(new ArrayList<String>(), new Integer(1));
    }
}

whereas this doesn’t

public class TestClass
{
    static void func(List o, Object s){
        o.add(s);
    }

    public static void main(String[] args){
        func(new ArrayList<String>(), new Integer(1));
    }
}

Why?

  • 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-23T22:50:04+00:00Added an answer on May 23, 2026 at 10:50 pm

    Why do we lose type safety when using List and not while using List<Object>? Aren’t they basically the same thing?

    No they are not the same thing.

    If you are providing an API,

    class API {
      static List<Object> getList() { ... }
      static void modifyList(List<Object> l) { ... }
    }
    

    and a client uses it improperly

    List<Integer> list = API.getList();
    API.modifyList(list);
    for (Integer i : list) { ... }  // Invalid
    

    then when your API specifies List<Object> they get a compile-time error, but they don’t when API.getList() returns a List and API.modifyList(list) takes a List without generic type parameters.

    EDIT:

    In comments you mentioned changing

    void func(List<Object> s, Object c) { s.add(c); }
    

    to

    void func(List s, Object c) { s.add(c); }
    

    so that

    func(new List<String>(), "");
    

    would work.

    That is violating type safety. The type-safe way to do this is

    <T> void func(List<? super T> s, T c) { s.add(c); }
    

    which is basically saying that func is a parameterized function that takes a List whose type can be any super class of T, and a value of type T, and adds the value to the list.

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

Sidebar

Related Questions

When I'm using something like git reflog, I lose my ability to type in
I'm trying to serialize a bool object using in the element text and I'm
I need to use a field of type numeric(11,9) in mysql. I'm using C
I'm using Chosen special input box for the Project Type input box on my
If I use the following code I lose the ability to right click on
I dislike it when I lose the session state of my ASP.NET web app
At what point does a MySQL database start to lose performance? Does physical database
I'm trying to decide whether or not it makes sense to go through the
Need to have a type-safe bag of items that all implement a generic interface.
I'm trying to build an object that looks something like this: public class MyObject

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.