Blog

Maria Jose Herrera

July 26, 2024

Spread the word


Share your thoughts

A Powerful addition to BoxLang’s Java interoperability!

We're excited to announce we’ve added more goodies to our BoxLang Java interop: method references and higher-order functions. These enhancements elevate Java integration, making your development experience more powerful and seamless than ever before.

Review Original Post

Java Method References

In CFML, you can grab a reference to a UDF or closure, pass it around, and invoke it. BoxLang extends this to Java methods, both instance and static. Check this out:


myInstance = new myClass();
myInstanceMethod = myInstance.myMethod;
myInstanceMethod();

And for static methods:


myStaticUDF = src.test.java.TestCases.phase3.StaticTest::sayHello;
myStaticUDF();

Treating Java Methods as Objects

In BoxLang, Java methods (both instance and static) are objects you can pass around, invoke, and use in higher-order functions. When you reference a method on a Java class without parentheses, you get a special Function instance. This lets you treat the method like any other function.

Try BoxLang

Static Method Reference Example

Capture the static valueOf() method from the Java String class and invoke it:


import java:java.lang.String;
javaStaticMethod = java.lang.String::valueOf;
result = javaStaticMethod("test"); // New string of "test"

Instance Method Reference Example

Capture the toUpperCase method from a String instance:


javaInstanceMethod = "my string".toUpperCase;
result = javaInstanceMethod(); // "MY STRING"

Using Java Methods with Higher-Order Functions

BoxLang lets you use Java methods directly in place of UDFs or closures for higher-order functions. For example, sort an array in reverse order using Java’s Collections.reverseOrder().compare method:


import java.util.Collections;
[1, 7, 3, 99, 0].sort(Collections.reverseOrder().compare); // [99, 7, 3, 1, 0]

Limitations and Considerations

There are some limitations. Many Java methods can’t be used with CF/BL’s reduceeachmap higher-order built-in functions (BIFs) because they strictly enforce the number of arguments. For example:


import java:java.lang.Math;
[1, 2.4, 3.9, 4.5].map(Math::floor); // Error because floor only accepts a single arg!

The Math.floor() method accepts a single argument, but arrayMap() passes three arguments, causing an error.

Two-Way Compatibility with Java

BoxLang’s ability to pass closures into Java methods that accept functional interfaces or lambdas enhances two-way compatibility between Java and BoxLang. Check out this example of using a Java Stream with a BoxLang lambda:


fruits = ["apple", "banana", "cherry", "ananas", "elderberry"];
result = fruits.stream()
  .filter(fruit -> fruit.startsWith("a"))
  .toList();

Here, we convert a BoxLang array into a Java Stream, use the Java filter() method with a BoxLang lambda, and collect the results into a list.

Conclusion

With BoxLang’s new support for Java method references and higher-order functions, Java interop just got a whole lot cooler. This feature lets you write cleaner, more powerful code by leveraging Java’s capabilities within BoxLang’s modern syntax. Give it a try and see how it can boost your development experience!

Try BoxLang

Add Your Comment

Recent Entries

Must-See Into the Box 2025 Sessions for CommandBox Users!

Must-See Into the Box 2025 Sessions for CommandBox Users!

Power Up your CommandBox experience and practices at Into the Box 2025

Want to get hands-on with the new CommandBox features or learn how others are pushing it to the next level? These are the must-see sessions at ITB 2025 if you're a CommandBox user:

Maria Jose Herrera
Maria Jose Herrera
April 21, 2025
Must-See ITB 2025 Sessions for TestBox Users!

Must-See ITB 2025 Sessions for TestBox Users!

Are you a fan of TestBox or looking to level up your testing game in 2025? Whether you're just getting started with unit testing or you're already building advanced specs for ColdBox and BoxLang apps, Into the Box 2025 has an exciting lineup tailored just for you. Into the Box 2025 has an exciting lineup tailored just for you. With the recent launch of TestBox 6.3.0 we have amazing new tools, features and tips and tricks to get your testing experience to the next level, review our sessions and test like a pro efficiently and easy!

From hands-on testing strategies to BoxLang innovations, here are the sessions you won’t want to miss this May — and why they matter to you as a TestBox user.

Maria Jose Herrera
Maria Jose Herrera
April 17, 2025
The Into the Box 2025 Agenda is LIVE and Done!

The Into the Box 2025 Agenda is LIVE and Done!

The wait is over! The official Into the Box 2025 agenda is now live — and it's packed with high-impact sessions designed for modern CFML and BoxLang developers. Whether you’re building APIs, modernizing legacy apps, diving into serverless, or exploring AI integrations, this is the conference you’ve been waiting for.

Here’s a look at what you can expect — categorized by key topics to help you plan your learning journey, there’s something for everyone covering modern CFML tools and BoxLang:

Maria Jose Herrera
Maria Jose Herrera
April 15, 2025