Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

OCP Chapter 3 - remove method in Queue and remove method in List  RSS feed

 
Ranch Hand
Posts: 149
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

in OCP page 168, there is the following question

               

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



I did not understand the part  :

Queue has only the remove by object method



I searched the API of Queue in the following link:
https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html#remove--

and I only found this remove method:

remove

E remove()


What does OCP mean when saying that Queue has only the remove by object method?


Thanks,
Ioanna
Retrieves and removes the head of this queue. This method differs from poll only in that it throws an exception if this queue is empty.

 
Sheriff
Posts: 4748
133
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 62
2
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This was careless naming IMO.  The Java Collections should never have sanctioned a (List).remove(int) to remove at a specific slot within the List.

It should have been named "removeAt(int)" or similar.

This probably happened because Java Collections far predated any consideration of Auto Boxing/Unboxing (Java 5 I believe), so it never occurred to anyone that an "intention" conflict might happen.

 
Ioanna Katsanou
Ranch Hand
Posts: 149
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello everyone and thank you for your answers !!!

In the first part of the code



How do we recognize that it uses E remove(int index) method from List interface instead of boolean remove(Object o) ???


Thanks,
Ioanna
 
Rancher
Posts: 2862
32
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How do we recognize


Read the API doc
 
Knute Snortum
Sheriff
Posts: 4748
133
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How do we recognize that it uses E remove(int index) method from List interface instead of boolean remove(Object o) ???


In the code q.remove(1), "1" is an integer literal.  That means its type is int.  Since int is a Java primitive and not an Object, the JVM will use E remove(int index).

This is a case of method overloading, that is, the methods have the same name but not the same arguments.  Which method is called is determined by the type and number of arguments.
 
Marshal
Posts: 58421
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Norm Radder
Rancher
Posts: 2862
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!