Is it safe to call the base java method for String in ColdFusion?

Adobe ColdFusion is built in Java. Almost all simple variables in CFML / CFSCRIPT are java.lang.String until the operation requires a specific type.

I always want to use startsWith() in String instead of the more cumbersome CFML option.

 left(str,4) EQ "test" 

However, what is the general consensus regarding using the underlying Java method in ColdFusion?

Would it be safer javacast() first var?

 javacast("String",x).startsWith("test"); 

What if CF is not built on top of Java?

thanks

+4
source share
2 answers

Yes, you can do this with Adobe ColdFusion and other CFML engines that are built in Java. This is actually easier than you thought.

 <cfset str = "hello what up" /> #str.startsWith("hello")# <!--- returns "YES" ---> <cfif str.startsWith("h")> This text will be output </cfif> #str.startsWith("goodbye")# <!--- returns "NO" ---> <cfif str.startsWith("g")> This text will NOT be output </cfif> 

This is possible because the CFML lines in ColdFusion are the same as the Java lines. You can use any native string method (Java.lang.String) in a CFML string.

If you didn’t guess, this also works with CFML arrays (some kind of list, maybe java.util.Vector) and structs (maybe java.util.Map). Experiment with data types and the cfdump tag, you will find many secrets.

One word of warning, this is not standard CFML, so if your main engine changes, including just updating to a new version, there is no guarantee that it will work anyway.

However, string.startsWith () is native to Java as well as .NET, so this will also work if your CFML engine is BlueDragon.NET. The only CFML engines he will not work on are ColdFusion 5 and previous.

Is it safe to use? I would say yes. While CFML engines run in Java or .NET, this is completely safe. This is undocumented, but easy to understand, so I would say I use it freely.

+6
source

I found that using built-in cf functions in most cases is faster than using their java copies, mainly because it is so expensive in cf updating java methods.

If you use .startsWith (), remember that it is case sensitive, while cf eq is not. The same goes for most other Java methods String -.endsWith () ,. contains (), etc.

If you can't combine the great features, since you can use your own java classes, mixing cf and java calls seems slow. If you are in some kind of Java code and you have a string and you call its startWith () method, it just executes. Done. In cf code you need javaCast or blindly hope that the variable is in the correct data type, which is risky with things like integer numeric strings, and when you call .startsWith (), there is a bunch of cf code that runs before it even comes to the java level in which slowness lives. For instance. Cf's dynamic arguments mean that it must check if there is a method for the supplied object with so many arguments, and from those data types (or compatible types). There is just a whole bunch of code that inevitably runs, overlapping two languages.

But do not trust our experience, orient yourself. eg.

 <cfscript> var sys = createObject( 'java', 'java.lang.System' ); var timer = sys.nanoTime(); // run some code here timer = sys.nanoTime() - timer; writeDump( var: timer ); </cfscript> 

If you use the Adobe cf engine, watch out for purely numeric strings, they bounce between java Doubles and Strings and don't make me start with serializeJSON () ...

+2
source

All Articles