Skip to main content
Ben Nadel at CFCamp 2023 (Freising, Germany) with: Michael Wiesmeyr and Cynthia Ramos and Raphael Mürwald and Marcel Mayr and Joseph Pernerstorfer and Daniel Pötscher and Andreas Breit and Luccas Hansch and Roman Schick and Christian Immitzer
Ben Nadel at CFCamp 2023 (Freising, Germany) with: Michael Wiesmeyr Cynthia Ramos Raphael Mürwald Marcel Mayr Joseph Pernerstorfer Daniel Pötscher Andreas Breit Luccas Hansch Roman Schick Christian Immitzer

Looping Over Java Arrays In ColdFusion

By on

This is a really small blog post, but something that I've been truly appreciating as of late. Previously, I've blogged about ColdFusion 10 and the ability to loop over queries using the for-in syntax; but, one for-in update that I totally missed during the ColdFusion 10 launch was the ability to loop over Java arrays using the same simplified syntax.

To demonstrate, I'm grabbing a Java array of type "java.lang.Byte[]" and looping over it using the new ColdFusion 10 syntax:


	// Get the typed Java array - [java.lang.Byte].
	bytes = javaCast( "string", "Funky Chicken!" ).getBytes();

	for ( byte in bytes ) {

		writeOutput( byte & ", " );



In ColdFusion 10, when I run this code, I get the following output:

70, 117, 110, 107, 121, 32, 67, 104, 105, 99, 107, 101, 110, 33

Now, I know this might seem like a totally uninteresting post; but, if I tried to do the same thing in ColdFusion 9, I would get the following ColdFusion error:

You have attempted to dereference a scalar variable of type class [B as a structure with members.

Fortunately, looping of Java arrays in ColdFusion 9 (and earlier) is still fairly easy; you just need to use a standard index-loop instead of a for-in loop:


	// Get the typed Java array - [java.lang.Byte].
	bytes = javaCast( "string", "Funky Chicken!" ).getBytes();

	for ( i = 1 ; i <= arrayLen( bytes ) ; i++ ) {

		writeOutput( bytes[ i ] & ", " );



That's all. Minor, but super useful!

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

Reader Comments


@Steve, yup, that makes the code run on Railo (I suspect has Java introspection disabled to ensure you can't create "dangerous" Java objects via sneaky means).

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