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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:08:08+00:00 2026-06-06T11:08:08+00:00

Possible Duplicate: Why is List<Number> not a sub-type of List<Object>? Isn’t String a subtype

  • 0

Possible Duplicate:
Why is List<Number> not a sub-type of List<Object>?

Isn’t String a subtype of Object in Java?

Then, how come I cannot pass an object of type List<String> into a function that accepts List<Object> as a parameter? I can, though, pass such an object into a function that accepts List as a parameter.

  • 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-06T11:08:12+00:00Added an answer on June 6, 2026 at 11:08 am

    Isn’t String a subtype of Object in Java?

    Yes it is, but that doesn’t make List<String> a subtype of List<Object>.

    Consider this example:

        List<String> l1 = new ArrayList<String>();
        List<Object> l2 = l1;  // This is a compilation error in real Java
        l2.add(new Integer(42));
        String oops = l1.get(0);
    

    If (hypothetically) List<String> was subtype of List<Object>, then you’d end up assigning a Integer to a String variable in the last statement. In short, we would have broken static type safety.


    I can, though, pass such an object into a function that accepts List as a parameter.

    Yes, that is true. But then you are dealing with raw types and you have to use explicit type casts when pulling objects out of the list. For example, the above would need to be rewritten as follows:

        List<String> l1 = new ArrayList<String>();
        List l2 = l1;
        l2.add(new Integer(42));
        String oops = (String) l1.get(0);
    

    Note that the type-cast means we’ve had to replace static type-safety with runtime type-safety. (And, of course, in this case the typecast will fail at runtime because the instance has the wrong type.)

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

Sidebar

Related Questions

Possible Duplicate: Type List vs type ArrayList in Java Why is it recommended to
Possible Duplicate: How to convert List<Integer> to int[] in Java? I have an ArrayList
Possible Duplicate: Getting JavaScript object key list How to iterate json data in jquery
Possible Duplicate: List of .Net Numeric Type Initialization Identifiers What means the D in
Possible Duplicate: Is List<Dog> a subclass of List<Animal>? Why aren't Java's generics implicitly polymorphic?
Possible Duplicate: Finding a single number in a list Given an array of numbers,
Possible Duplicate: Implement linked list in php We can make link list in Java,
Possible Duplicate: Split string to equal length substrings in Java Given the following utility
Possible Duplicate: Random number generator not working the way I had planned (C#) I
Possible Duplicate: list.clear() vs list = new ArrayList<int>(); I have a list: List<Integer> l1

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.