Coldfusion Strings: Java Objects In Disguise

I was talking with Luis the other day about some ColdFusion and framework specific stuff that we would like to see in the upcoming ColdFusion 9. During the conversation Luis pointed something out to me that I hadnt known before. After using ColdFusion for over 10 years, Im almost ashamed to admit that I didnt know this. But then again, I didnt have any OO programming knowledge and other than classic ASP I had no programming experience at all when I started working with ColdFusion.
Strings in ColdFusion are not just Strings. They are actually Java strings. Duh you say? I guess I got so used to CF not being typed that I never really thought about it. So for those as clueless as I am, heres what that means.Anytime you create a simple string variable in ColdFusion, it inherits methods from the Java String Class. Lets look at an example:

#myVar.toUpperCase()#

That code would obviously output the variable “test” in all uppercase. Since ColdFusion automatically types the variable for us, we can take advantage of the inherited Java methods in our ColdFusion code. Heres another example:

#myVar#

Strings are not the only thing that ColdFusion types for us automatically. Integers are handled as well. The following snippet would return a boolean of false:

#myInt.equals("4")#

And the last example would return a value of -5:

#myInt.compareTo("7")#

Im not really sure how useful any of this is, but it is quite cool to play with! The simplicity of ColdFusion can make you forget that there really is an Object Oriented language called Java lurking beneath the covers.

5 thoughts on “Coldfusion Strings: Java Objects In Disguise”

  1. Fun stuff, huh? 😉 Actually, every variable and object etc. in CF (since CFMX) is Java internally. For any variable, you can use getClass().getName() to determine what the actual internal class is. So, if you have a variable named foo just refer to foo.getClass().getName(). You can also dump getClass() itself to see what methods the internal Java object exposes.

    — Ben

Leave a Reply

Your email address will not be published. Required fields are marked *


*