The explanation on page 555 of the book says that:
LinkedList implements both List and Queue. The List interface has a method to
remove by index. Since this method exists, Java does not autobox to call the other method.
Queue has only the remove by object method, so Java does autobox there. Since the number
1 is not in the list, Java does not remove anything for the Queue
If you look in the API for Queue, you see first the remove() with no arguments that you mentioned. However, if you look below at the methods inherited by Collections, you will see a method with this signature:
boolean remove(Object o)
This is the remove by Object the book is speaking of.
All things are lawful, but not all things are profitable.
It was easier in the days of Java1.4; you didn't have boxing to contend with. If you called remove(999); in Java1.4, it counted as an int and in Java 8 it is still an int. You can't allow the behaviour of that sort of thing to change.
posted 1 year ago
The OP asked:
How do we recognize that it uses
How can that be determined other than by reading the API doc?
If the method is documented as taking an int, then the int is used.
If the method does not take an int, the it will be boxed into an object.
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database