Win a copy of Learning Java by Building Android Games this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

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

 
Ranch Hand
Posts: 153
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: 5253
141
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: 72
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: 153
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: 3276
33
  • 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: 5253
141
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: 60732
189
  • 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: 3276
33
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!