Results 1 to 4 of 4
  1. #1
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default Declaring an ArrayList

    HI,

    Please review the code below:

    Java Code:
    Collection <String>arrayList1 = new ArrayList<String> ();
    ArrayList <String>arrayList2 = new ArrayList<String> ();
    What is the real difference between arrayList1 and arrayList2? Both works the same ????

    Cheers.

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default Same instances

    Hello bugger.

    arrayList1 and arrayList2 are both instances of ArrayList, but they are of different type. arrayList1 can contain a Collection object. Although arrayList1 is actually an ArrayList, the compiler will see it as an object of type Collection. To use arrayList1 as an ArrayList you must first cast it. arrayList2 is an object of type ArrayList and no cast is necessary. But, in essence they are both still ArrayList objects.

    Does that help? :D
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  3. #3
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default

    Thanks Tim. It means both can only store ArrayLists. Even arrayList1 cannot store other collections. Is this so?

  4. #4
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    Quote Originally Posted by bugger View Post
    Thanks Tim. It means both can only store ArrayLists. Even arrayList1 cannot store other collections. Is this so?
    That is not true. arrayList1 can store any instance of Collection, but arrayList2 can only store instances of ArrayList or any subclasses of it. For example:
    Java Code:
    Collection <String> test1 = null;
    ArrayList <String> test2 = null;
    The following is okay
    Java Code:
    test1 = new ArrayList<String>();
    test1 = new Vector<String>();
    test2 = new ArrayList<String>();
    The following is NOT okay
    Java Code:
    test1 = new Integer(10);
    test2 = new Vector<String>();
    These are polymorphism concepts. I am sorry if I confused you. :o I'm trying my best to explain this.
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

Similar Threads

  1. Replies: 2
    Last Post: 04-21-2008, 11:43 AM
  2. Declaring a Queue
    By rhm54 in forum New To Java
    Replies: 1
    Last Post: 03-21-2008, 05:02 AM
  3. Declaring Vector
    By mew in forum New To Java
    Replies: 1
    Last Post: 12-05-2007, 08:14 PM
  4. Declaring Interface
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-08-2007, 08:41 AM
  5. Declaring Enumeration
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-04-2007, 05:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •