Skip to main content
Ben Nadel at the jQuery Conference 2010 (Boston, MA) with: Alex Sexton
Ben Nadel at the jQuery Conference 2010 (Boston, MA) with: Alex Sexton

Calling Java Class Methods Directly From ColdFusion Objects

By
Published in Comments (4)

I was on my way back from vacation yesterday, driving down Route 95 South, when suddenly it hit me: Why not just call Java methods directly from the ColdFusion objects we use? Let's look at string manipulation, since it is a large part of programming. In the past, if I wanted to create a Java String object from a ColdFusion String object, I would instantiate a new Java String object:

<cfset jstrText = CreateObject(
	"java",
	"java.lang.String"
	).Init(
		strText
	) />

This would take the ColdFusion string "strText" and use it to construct a new Java String object "jstrText". But, what I realized on the road was that the ColdFusion object ALREADY IS a Java String object. The Java String object exists - there is no need to create a new one. What's not obvious is that those Java methods are available from the ColdFusion String object.

One of the big reasons I like Java String objects is that their Regular Expression replaces are MUCH faster than that of ColdFusion. So, let's take this text for example. I am going to build a ColdFusion string:

<!--- Save text in a buffer variable. --->
<cfsavecontent variable="strText">

	As Anna stepped out of the water into the
	cool light of the moon, I could just make out
	curves of her hip through the wet threads of
	her summer dress.

</cfsavecontent>

Now, you can run regular expression replaces the ColdFusion way:

<cfset strResult = REReplace(
	strText,
	"([a-z]{1,3})",
	"{\1}",
	"ALL"
	) />

BUT, you can also call the Java methods directly as well:

<cfset jstrResult = strText.ReplaceAll(
	"([a-z]{1,3})",
	"{$1}"
	) />

How cool is that? I'll tell you: wicked cool! Not only that, the Java String::ReplaceAll() method is faster than the ColdFusion REReplace() method. And, we didn't incur the overhead of instantiating a new Java String object to work with the regular expressions. Again, wicked cool!

Want to use code from this post? Check out the license.

Reader Comments

30 Comments

That's a great idea. I've written many CF apps over the years that had a lot of regex replacement. This can get to be a significant source of overhead. Have you seen or personally done any tests to measure the performance increases of Java regex replacement vs ColdFusion?

15,848 Comments

Jeremy,

In my experience, yes, Java regular expressions are faster. But, honestly, it's not just about the speed; it about the flexibility. Java regular expression are simply more powerful than ColdFusion regular expressions. Of particular note is that Java regular expressions can handle positive and negative look behinds, which I believe ColdFusion can handle none.

Now, don't get me wrong, I use REFind(), REReplace() all the time. But there are certainly situations where the Java regular expression is awesome.

172 Comments

I'll add, in response to Jeremy's comment, that the biggest source of performance problems with regular expressions is developers who don't know how to write regular expressions in ways that optimize the performance.

15,848 Comments

Steve,

I just got finished commenting on another of your comments.... I simply didn't think much about optimization because I didn't realize that tweaks could really be done. My eyes have been opened.

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel