Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Array List Doubt

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Array List Doubt

    Can Any one tell me the difference between these two statements

    List list = new ArrayList();
    ArrayList list = new ArrayList();

    In an interview, i got this question..........please answer it.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Array List Doubt

    Ok, well List is an interface, which means it cannot be initalized. ArrayList implements List, so that's where it gets some of its methods from.

    I think (not entirely sure on this), but by saying List list new ArrayList(), you are creating a new ArrayList, but limiting it to the methods given to it by the List interface. Whereas saying ArrayList list = new ArrayList(), you are creating a new ArrayList with full access to the ArrayList methods.

    I think thats it, but I'm not entirely sure. I learned the purpose of that like a year or two ago, and I use it alot when I create my own Listeners, but I cant exactly remember what it restricts. The more I think about it, the more I think I may be wrong.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Array List Doubt

    List is an interface which ArrayList implements. Doing so allows you access to all the implemented methods provided by the List interface, without worrying about how the List is implemented. This is a very common and powerful method to pass around objects, as it helps make a program loosely coupled. A simple example might be the following: you've written an class (call it say ListReturn) which contains a method that returns an ArrayList - call this method listReturn(). Considering that many other objects utilize listReturn(), returning an ArrayList makes all those methods dependent upon ArrayList. Now suddenly you need to change your program to a multithreaded environment and need to synchronize things (ArrayList is not syncrhonized), returning an ArrayList from listReturn() forces you to update all the other classes, whereas returning just a List allows you to make the change in a single object - ListReturn (for example using Collections.synchronizedList or a Vector). Just another form of encpaculation
    Last edited by copeg; July 12th, 2010 at 10:23 AM.

Similar Threads

  1. Doubt Regarding Printing an Array List
    By reeceypp in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 20th, 2010, 03:11 AM
  2. Doubt regarding LIST
    By puneetsr in forum Collections and Generics
    Replies: 1
    Last Post: February 23rd, 2010, 04:19 PM
  3. JComboBox doubt
    By rajaramesh.tadi in forum AWT / Java Swing
    Replies: 0
    Last Post: August 24th, 2009, 08:18 AM
  4. Which javadoc tag is used to denote a comment for a method parameter?
    By TARUN JORA in forum Java Theory & Questions
    Replies: 2
    Last Post: May 15th, 2009, 04:30 PM
  5. Replies: 1
    Last Post: November 22nd, 2008, 01:32 PM