Java programming with lambda expressions

A mathematical example demonstrates the power of lambdas in Java 8

1 2 Page 2
Page 2 of 2

As a final thought, although I prefer writing the interface DoubleFunction because I think that it makes the code easier to understand, even that interface can be eliminated. Java 8 has a new package, java.util.function, that contains a number of commonly used functional interfaces. Many are expressed using generics, but there are specializations for primitive types. I'll leave the use of one of these interfaces in place of DoubleFunction as the proverbial exercise for the reader. (For a hint, look closely at DoubleUnaryOperator in java.util.function. If you get stuck, see class Simpson2 in the source code provided with this article.)

In conclusion

Overall I have found it a pleasure to code in Java, and it has been my preferred programming language for more than 15 years. There are (still) a few places where Java's syntax seems awkward, however. With the addition of lambda expressions, Java 8 will correct one of them. Taken together, the major points of this article remind me of Cay Horstmann's so-called March of Progress, a portion of which is copied below with his permission.

The March of Progress, by Cay Horstmann

1980: C

printf("%10.2f", x);

1988: C++

cout << setw(10) << setprecision(2) << showpoint << x;

1996: Java

java.text.NumberFormat formatter = java.text.NumberFormat.getNumberInstance();
formatter.setMinimumFractionDigits(2);
formatter.setMaximumFractionDigits(2);
String s = formatter.format(x);
for (int i = s.length(); i < 10; i++) System.out.print(' ');
System.out.print(s);

2004: Java

System.out.printf("%10.2f", x);

Learn more about this topic

1 2 Page 2
Page 2 of 2