4

Lately I started studying about different interesting concepts that exist in languages other than Java. Since the only language I've ever programmed with is Java, a lot of these concepts are very new to me. So this question may be very naive :) .

I learned recently about first class functions and function pointers. Why are there no function pointers in Java? Or at least some variation of them, like delegates in C#?

Maybe it's just the excitement of learning about this concept, but it seems to me like it could be a powerful feature in the language.

closed as primarily opinion-based by Robert Harvey, Jarrod Roberson, user40980, GlenH7, gnat May 23 '14 at 3:37

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    James Gosling probably figured you wouldn't mind typing 5 lines of code to create an anonymous class. As @FrustratedWithFormsDesigner points out, Java 8 now has lambda expressions and "method expressions" which are essentially a way to use methods as first-class functions. If you really want to play around with first-class functions, you should look into functional programming languages like Standard ML. – Doval May 22 '14 at 20:08
  • 4
    All questions of the form "Why did they not put [some feature] in [some language]" always have the same answer: the benefit did not outweigh the cost. Consider the philosophy that the designers of Java had; it was supposed to be the kinder, gentler, safer version of C++. Pointers don't really fit that philosophy. – Robert Harvey May 22 '14 at 20:08
  • 2
    In short, they are unsafe, and Java is supposed to be a safe language. coderanch.com/t/370799/java/java/java-pointer-concept – Robert Harvey May 22 '14 at 20:11
  • 2
    @RobertHarvey Function pointers, even the untamed C version, are very much unlike ordinary C pointers. You can't reasonably do arithmetic on them (I don't know if it's outright illegal in C, but even if it's allowed it serves no purpose and can safely be removed), and lifetime issues such as use-after-free are not possible because functions life forever. – user7043 May 22 '14 at 20:37
  • 2
    @StevenBurnap Arithmetic on function pointers means: Given fp being a function pointer, do fp + 1 or such and get another (valid) function pointer. When you use a (C-style) array of function pointers, you do pointer arithmetic on the pointer to the first element of that array, which is an ordinary pointer-to-T (for T = some function pointer). The former is not possible (function layout in memory is not specified, you can't even get the "size" of a function); the latter is possible and useful but does not involve pointer arithmetic on function pointers. – user7043 May 22 '14 at 20:41
4

Function pointers by themselves are more or less a workaround for the lack of first-class functions. But Java does have first-class functions as of Java 8 (lambdas), and it had was of (somewhat) easily faking them since Java 1.1 (anonymous inner classes).

But actually, Java (or more precisely the JRE) also has a concept that more directly corresponds to function pointers than lambdas or anonymous inner classes: MethodHandles. It's a handle for a method. Which is really not much different than a pointer to a function. They were added in Java SE 7.

2

Short answer: because Java has other ways to accomplish the same thing, and the language goes out of its way to hide pointers behind references which do not have pointer semantics.

Long answer:

The primary benefit of a function pointer in C or C++ is for an external library to plug in functionality at runtime, because a class is not known at compile time. Consider a WidgetFactory in an application. It is extensible: one may subclass it to produce new widget types. However, it is not possible for a C++ application to create a PluginWidgetFactory because it has not been written yet.

Enter function pointers: after loading a DLL/SO file dynamically, that library will call a function in the main application and register itself as a factory. It will provide a function pointer to say "call this function and I will return my subclass of WidgetFactory that your compiler knew nothing about."

In Java, you would use reflection. The Java WidgetFactory would check a system property with the class name, and load the PluginWidgetFactory dynamically using reflection.

While Java does not support function pointers, it can accomplish the same end goal using different mechanisms. The creators of Java felt these other ways were better due to their goal of hiding pointers.

Not the answer you're looking for? Browse other questions tagged or ask your own question.