I just wanted to list some of my favourite Java shortcuts that I like to use when using ColdFusion
java.lang.String.length() – Yes I could do this with Len() from cf, but typing myString.length()
seems to be so much easier and faster for me.
java.lang.String.getBytes() – a useful way of working out how many bytes in a string. This can be interesting to use to check approximatley how much memory you are using when storing long strings in a shared scope.
java.lang.reflect.Array – The man when it comes to dealing with Java native arrays.
java.util.Date.before() and .after() – Yes, I can use dateCompare, but I simply find if(myDateOne.after(myDateTwo)) {..}
so much easier to read (and all CF date objects are Java date objects).
java.util.Vector.addAll() – Since all CF arrays are vector, this is a easy way of adding an entire array into another without having to manually enter every single item. i.e.
<cfscript>
a = arrayNew(1);
a[1] = "1";
a[2] = "2";
a[3] = "3";
b = ArrayNew(1);
b.addAll(a);
</cfscript>
I think that's all the ones I usually use at the moment – what other Java snippets do you use to save yourself time?
Comments
java.lang.String.split("any goddamn delimeter you want")
(I hate that listToArray only can take one character delimeters)
Pete,
Are all of the methods for those packages (class) available or our their limitations?
-Pat
Question – why do you say you need addAll? You are aware of arraySum, right?
Raymond –
to c/p
ArraySum
Array sum function.
The sum of values in an array. If the array parameter value is an empty array, returns zero.
addAll doesn’t add all the values togoether – it appends all the objects of the first array INTO the second array.
Did you mean a different function?
Oops. You are right. It’s what I get for reading blogs before coffee.
That’s okay – for a minute there I thought I had gone ‘woah! new thing’.. only to find yet again I hadn’t read the docs properly ;o)
The StringBuffer class concatenation has been the most important use of Java with in CF I have found/used
http://www.findarticles.com/cf_dls/m0MLU/1_6/112456068/p1/article.jhtml
Wouldn’t array addall function seems like using duplicate or am I missing something?
Qasim –
addAll() *could* be used that way, particularly in the case of an array of CFCs (except each CFC would be passed by refernce, not by value – and you can’t duplicate CFCs as per previous post).
Or it could be used in an instance where you have 2 already established arrays, and simply want to join them into a single one quickly and easily. All depends on what you need.
Daniel –
Nice performance results on using StringBuffer! Very good to know!
"Nice performance results on using StringBuffer! Very good to know!"
Yeah, I was very fortunate to have found the article in CFDJ a week before someone asked us to look into speeding up the creation of a CSV that was taking around 5-10 minutes. It was a simple query loop to build up a string but as the article explains, the CF variable is being created for every concatenation (read: creating a variable thousands of times = takes long time). Use of Java StringBuffer class concatenation dropped the CSV creation time down to about 15 seconds.
I have found that using the java classes directly using cfobject is very fast but if you call the java methods on CF objects (like x.add() on a CF array) it is very slow. In tests i did, the java add() method was more than 10 times slower than using CFs ArrayAppend() !! Not sure why this is the case 🙁
Probably because CF arrays are java vectors. There’s probably a conversion process taking place to get the vector into an array for every the java function.