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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:46:05+00:00 2026-05-26T00:46:05+00:00

For example: List<String> list = new ArrayList<String>(); vs ArrayList<String> list = new ArrayList<String>(); What

  • 0

For example:

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

vs

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

What is the exact difference between these two?

When should we use the first one and when should we use the second?

  • 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-26T00:46:06+00:00Added an answer on May 26, 2026 at 12:46 am

    The first form is the most desirable one because you hide the implementation (ArrayList) from the rest of your code and ensure your code only works with the abstraction (List). The advantage of this is that your code will be more generic and therefore easier to adapt, for example when you change from using an ArrayList to a LinkedList, Vector or own List implementation. It also means local changes are less likely to cause changes in other parts of your code (‘ripple-effect’), increasing your code’s maintainability.

    You need the second form when you want to do things with your variable that are not offered by the List interface, for example ensureCapacity or trimToSize

    EDIT: extra explanation of changing the implementation

    Here is an example of declaring a variable as a Collection (an even more generic interface in java.util):

    public class Example {
    
        private Collection<String> greetings = new ArrayList<String>();
    
        public void addGreeting(String greeting) {
            greetings.add(greeting);
        }
    }
    

    Now suppose you want to change the implementation in order to store unique greetings, and therefore switch from ArrayList to HashSet. Both are implementations of the Collection interface. This would be easy in this case because all the existing code treats the greetings field as a Collection:

    public class Example {
    
        private Collection<String> greetings = new HashSet<String>();
    
        public void addGreeting(String greeting) {
            greetings.add(greeting);
        }
    }
    

    There is an exception. If there is code which casts the greetings field back to its implementation, this makes that code ‘implementation-aware’, violating the information-hiding you tried to achieve, for example:

    ArrayList<String> greetingList = (ArrayList<String>) greetings;
    greetingList.ensureCapacity(42);
    

    Such code would cause a runtime error ‘java.lang.ClassCastException: java.util.HashSet incompatible with java.util.ArrayList’ if you change the implementation to HashSet, so this practice should be avoided if possible.

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

Sidebar

Related Questions

Given the following simple example: List<string> list = new List<string>() { One, Two, Three,
For example: string element = 'a'; IEnumerable<string> list = new List<string>{ 'b', 'c', 'd'
For example, to denote a String I could use: {string,hjggjhhggJ} and a list would
Consider: List<String> someList = new ArrayList<>(); // add "monkey", "donkey", "skeleton key" to someList
I have list of String. List<String> elements= new ArrayList<String>(); when I wanna add new
considering this example: public static void main(final String[] args) { final List<String> myList =
I am looking at this example: List<Product> products = Product. GetSampleProducts() ; products.Sort( (first,
Take the following generics example import java.util.List; import java.util.ArrayList; public class GenericsTest { private
I have two arraylist like this ArrayList<Paragens> paragens = new ArrayList<Paragens>(); ArrayList<Rotas> rotas =
In my admin view I would like several sub directories for example: admin/users/list.aspx I

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.