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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:57:58+00:00 2026-06-12T14:57:58+00:00

Possible Duplicate: List versus ArrayList as reference type? I read somewhere in StackOverflow that

  • 0

Possible Duplicate:
List versus ArrayList as reference type?

I read somewhere in StackOverflow that this:

List<String> foo = new ArrayList<String>();

is the best practice for initialization of ArrayList. And that this:

ArrayList<String> foo = new ArrayList<String>();

should be avoided. Is this correct? If yes can someone explain why is it so?

Thanks,
EG

  • 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-12T14:57:59+00:00Added an answer on June 12, 2026 at 2:57 pm

    This is how most developers use ArrayList (myself included), and is a general tenet of polymorphism. You should use the highest class/interface in the class hierarchy that you can while still having all your functionality available.

    Generally, this makes your code more flexible, and you can switch out implementations more easily since you know that programming with List as the type hasn’t bound you to using any methods that are only on ArrayList and not available on, say, a LinkedList.

    For example, if I did:

    LinkedList<String> strings = new LinkedList<String>();
    String s = strings.pop();
    

    pop is exclusive to LinkedList, so I can’t just change it directly to an ArrayList. I’d have to also change my code:

    ArrayList<String> strings = new ArrayList<String>();
    String s = strings.remove(strings.size() - 1);
    

    But if I had just used the interface, I would’ve never used pop in the first place, so I could’ve just changed strings to whatever implementation I wanted.

    This is especially important when programming APIs. If your methods are publicly accessible and will be used by other developers, then you need to make sure that you aren’t restricting what kind of lists your methods take unless you have a good reason for doing so, especially since they may have a good reason for not wanting to use ArrayList.

    If you’re doing nothing but iteration and adding/removing, you could even go up to Collection. Here you start to lose context though, since List tells you it may contain duplicates and is ordered manually. But if this isn’t particularly important and you can use Collection, you can even replace the lists with implementations of Set.

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

Sidebar

Related Questions

Possible Duplicate: List versus ArrayList variable type? I'm intialising a HashSet like so in
Possible Duplicate: list.clear() vs list = new ArrayList<int>(); I have a list: List<Integer> l1
Possible Duplicate: Why is List<Number> not a sub-type of List<Object>? Isn't String a subtype
Possible Duplicate: Type List vs type ArrayList in Java Why is it recommended to
Possible Duplicate: Java: convert List<String> to a join()d string Having this: List<String> elementNames =
Possible Duplicate: Collection<T> versus List<T> what should you use on your interfaces? Consider this
Possible Duplicate: Python list append behavior Why does this code: x = [[]]*3 x[0].append('a')
Possible Duplicate: How to list my app downloads I created an android app that
Possible Duplicate: Get a list of dates between two dates This is my sql:
Possible Duplicate: How to convert List<Integer> to int[] in Java? I have an ArrayList

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.