Calling Java Class Methods Directly From ColdFusion Objects

Posted August 16, 2006 at 8:44 AM by Ben Nadel

Tags: ColdFusion

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!




Reader Comments

Aug 24, 2006 at 3:01 PM // reply »
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?


Aug 25, 2006 at 7:59 AM // reply »
74 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.


Feb 2, 2007 at 9:56 PM // reply »
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.


Feb 3, 2007 at 7:50 PM // reply »
11,238 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.


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 17, 2013 at 7:42 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
Ben - thanks so much for posting these Angular articles and findings, they've been a huge help towards learning one of the more 'complex' JavaScript frameworks out there (IMO). I have been using Angu ... read »
May 16, 2013 at 5:01 PM
UPDATE: Parsing CSV Data Files In ColdFusion With csvToArray()
Your code was the closest thing I've found to obtaining some direction for converting ISO fields to values that CF can translate properly. Thank you for posting! ... read »
May 15, 2013 at 10:37 PM
Very Simple Pusher And ColdFusion Powered Chat
hi id making plz easy ... read »
May 15, 2013 at 6:07 PM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
Ben, you once again saved my bacon at work. Thank you, thank you, thank you! ... read »
May 15, 2013 at 4:15 PM
What If All User Interface (UI) Data Came In Reports?
@Josh, Thanks! @Ben, I definitely recommend the David West book "Object Thinking" I've been quoting from. It goes deeply into the philosophy and history of OO programming. His breadth ... read »
May 15, 2013 at 11:36 AM
Ask Ben: Print Part Of A Web Page With jQuery
I found this helpfull when you need to keep (refresh) the original parent page after closing the iframe child print dialog (Hoping you're not using a form at this time so it won't submit again): On ... read »
May 14, 2013 at 7:13 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, If there's any books you'd recommend on the subject of domain modelling, I'd love to hear it. I just downloaded the free PDF of "Domain Driven Design Quickly". Figured I'd give it ... read »
May 14, 2013 at 6:57 PM
The UX Of Prototyping: Low-Fidelity Is The New High-Fidelity
@Phillip, I'm not sure I follow what you mean? Are you saying that you looked at the list of widgets provided by the jQuery UI and let that be your style guide? ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools