Why would you need function pointers in such a heavily OOP language?
For the same reason that they're desirable in C++ (or classes with an <tt>operator()</tt> overload (i.e. functors), which the language treats almost identically, and again which Java has no direct equivalent to). A common use of this in C++ is for predicates -- a function returning true or false, to determine if some action should be performed (for instance, <tt>std::list::remove_if()</tt> takes a predicate that's used to determine if an element gets removed from the list or not). The <tt>remove_if()</tt> function takes either a function pointer or a functor (because the syntax for calling through a function pointer is the same as invoking a class's <tt>operator()</tt> the language lets you use them fairly interchangably).
Java can't do either of these things -- it has no function pointers and it has no function-like objects. It could partially redeem itself if it had some kind of IPredicate, with a single Test method taking an Object; instead of a functor class, one would write a class that implements IPredicate, that did something appropriate to the parameter it was passed. It wouldn't be perfect, but it would work. Alas, there's no such thing, and the designers of the Collections API deliberately left it out; instead, they say that people wanting such functionality should built it atop iterators, which is a rather lame cop-out. The point is to express a high-level idea -- "remove elements if this condition is satisfied" -- not to have to iterate through the list manually, removing the elements by hand.
And then there's callbacks. Yes, inner classes are used for many of the same things, but callbacks are often a neater way to do this -- this is reflected, IMO, by the callback interface of javax.security.auth. Callbacks are an effective and familiar way of achieving certain tasks, and again, function pointers and/or functors are the way to do them.
I thought everything in Java, except for maybe int and char, are already references, so all function calls using object parameters are already pass by ref?
No, it doesn't. It has pass-by-value, and pass-by-value alone.
The confusion lies in objects; object variables are created on the heap (with <tt>new</tt>) and the actual variables are pointers. These pointers are also passed by value; in many places, the existence of the pointers is hidden, which gives the illusion of pass-by-reference,
but it is not pass-by-reference.
Demonstration of this(for appropriate classes with appropriate methods and constructors):
Code:
public void foo(Bar b)
{
b = new Bar("quux");
}
Bar bar = new Bar("baz");
bar.print();
foo(bar);
bar.print();
The output will be:
The languages passes the pointer
by value to the function. The function modifies that passed-in copy of the pointer, not the actual pointer itself.
Notice how it works identically to pass-by-pointer in C++:
Code:
void foo(Bar* b)
{
b = new Bar("quux");
// ignore the leak
}
Bar* bar = new Bar("baz");
bar->print();
foo(bar);
bar->print();
The output will be:
because on calling the function the pointer gets copied.
Contrast this with C++'s pass-by-reference:
Code:
void foo(Bar*& b)
{
b = new Bar("quux");
}
Bar bar = new Bar("baz");
bar->print();
foo(bar);
bar->print();
The output will be:
This is proper pass-by-reference. Java can't do it. It only ever passes by pointer. It hides those pointers, yes, so you can't do pointer arithmetic or bad casts on them, but they are pointers, not references, which is why they behave the way they behave. The variables that you create are pointers to objects in Java's garbage-collected heap, rather than synonyms for the objects themselves.
This pass-by-value nature is made explicit here:
When the method or constructor is invoked, the values of the actual argument expressions initialize newly created parameter variables, each of the declared Type, before execution of the body of the method or constructor.
(emphasis mine). New pointer variables are created. Now, because they still point at the same objects, calling methods on them will indeed modify the object they're pointing at -- hence the mistaken assumption that it's pass-by-reference. But an assignment will fail to have any side-effects, because it clobbers the *copy* of the pointer, and no more.