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.


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

Results 1 to 3 of 3

Thread: Do not understand this API

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Thanked 0 Times in 0 Posts

    Default Do not understand this API

    Why do addAll and removeAll methods contain different signature as highlighted below. Same thing with add and remove.

    boolean addAll(Collection<? extends E> c)

    boolean removeAll(Collection<?> c)

    boolean add(E e)

    boolean remove(Object o)

    When we cant add other than the elements of type E or those which extends E, then why should we have removeAll/remove generified?


    List<Number> numbers = new ArrayList<Number>();


    in the code above why line3 will give you the compiler error but not line 4. Please explain.

  2. #2
    Super Moderator
    Join Date
    Jun 2013
    So. Maryland, USA
    My Mood
    Thanked 698 Times in 680 Posts

    Default Re: Do not understand this API

    I would say the design gives the programmer maximum flexibility while enforcing Java's rather strict typing requirements.

    As for your code example and final question: Obviously, "hello" is not a Number so cannot be added to the List of Numbers which is limited to containing object of type Number. The remove() method preserves the ability to remove any object from the List, regardless the type. That seems like a good thing.

    Edit: But if this question is to answer a test question, you'd better read the book to find the real answer. I''m guessing.

  3. #3
    Join Date
    Jul 2013
    Thanked 18 Times in 17 Posts

    Default Re: Do not understand this API

    If you are able to do then you can have a look at the source code of the add() and remove() methods to see the logic they have used.
    The source code shall be available as a zip file in your jdk folder.


Similar Threads

  1. Please Help me to understand
    By Theillusion in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 17th, 2013, 03:09 AM
  2. Please help to Understand x=++x+ + +x+x++
    By rayan2004 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: December 2nd, 2012, 07:47 PM
  3. Can someone help me understand toString()?
    By Psychotron in forum Java Theory & Questions
    Replies: 10
    Last Post: January 18th, 2012, 10:43 PM
  4. Help me to understand this
    By Madhushan in forum Java Theory & Questions
    Replies: 2
    Last Post: September 10th, 2011, 08:47 AM
  5. Difference between Speech API and Sound API
    By zeeshanmirza in forum Java SE APIs
    Replies: 1
    Last Post: October 22nd, 2009, 12:22 AM