ColdFusion does not use explicit types for variables, while Java is strongly typed. However, ColdFusion data does use a number of underlying types to represent data.
Under most situations, when the method names are not ambiguous, ColdFusion can determine the data types that are required by a Java object, and often it can convert ColdFusion data to the required types. For example, ColdFusion text strings are implicitly converted to the Java String type. Similarly, if a Java object contains a doIt method that expects a parameter of type int, and CFML is issuing a doIt call with a CFML variable x that contains an integer value, ColdFusion converts the variable x to Java int type. However, ambiguous situations can result from Java method overloading, where a class has multiple implementations of the same method that differ only in their parameter types.
The following sections describe how ColdFusion handles the unambiguous situations, and how it provides you with the tools to handle ambiguous ones.
Whenever possible, ColdFusion automatically matches Java types to ColdFusion types.
The following table lists how ColdFusion converts ColdFusion data values to Java data types when passing arguments. The left column represents the underlying ColdFusion representation of its data. The right column indicates the Java data types into which ColdFusion can automatically convert the data:
CFML |
Java |
---|---|
Integer |
short, int, long (short and int might result in a loss of precision). |
Real number |
float double (float might result in a loss of precision. |
Boolean |
boolean |
Date-time |
java.util.Date |
String, including lists |
String short, int, long, float, double, java.util.Date, when a CFML string represents a number or date. boolean, for strings with the value Yes, No, True, and False (case-insensitive). |
Array |
java.util.Vector (ColdFusion Arrays are internally represented using an instance of a java.util.Vector object.) ColdFusion can also map a CFML array to any of the following when the CFML array contains consistent data of a type that can be converted to the Java array's data type: byte[], char[], boolean[], int[], long[], float[], double[], String[], or Object[]. When a CFML array contains data of different of types, the conversion to a simple array type might fail. |
Structure |
java.util.Map |
Query object |
java.util.Map |
XML document object |
Not supported. |
ColdFusion component |
Not applicable. |
The following table lists how ColdFusion converts data returned by Java methods to ColdFusion data types:
Java |
CFML |
---|---|
boolean/Boolean |
Boolean |
byte/Byte |
String |
char/Char |
String |
short/Short |
Integer |
int/Integer |
Integer |
long/Long |
Integer |
float/Float |
Real Number |
double/Double |
Real Number |
String |
String |
java.util.Date |
Date-time |
java.util.List |
Comma-delimited list |
byte[] |
Array |
char[] |
Array |
boolean[] |
Array |
String[] |
Array |
java.util.Vector |
Array |
java.util.Map |
Structure |
Resolving ambiguous data types with the JavaCast function
You can overload Java methods so a class can have several identically named methods. At runtime, the JVM resolves the specific method to use based on the parameters passed in the call and their types.
In the section The Employee class, the Employee class has two implementations for the SetJobGrade method. One method takes a string variable, the other an integer. If you write code such as the following, which implementation to use is ambiguous:
<cfset emp.SetJobGrade("1")>
The "1" could be interpreted as a string or as a number, so there is no way to know which method implementation to use. When ColdFusion encounters such an ambiguity, it throws a user exception.
The ColdFusion JavaCast function helps you resolve such issues by specifying the Java type of a variable, as in the following line:
<cfset emp.SetJobGrade(JavaCast("int", "1"))>
The JavaCast function takes two parameters: a string representing the Java data type, and the variable whose type you are setting. You can specify the following Java data types: boolean, int, long, float, double, and String.
For more information about the JavaCast function, see the CFML Reference.
You handle Java exceptions just as you handle standard ColdFusion exceptions, with the cftry and cfcatch tags. You specify the name of the exception class in the cfcatch tag that handles the exception. For example, if a Java object throws an exception named myException, you specify myException in the cfcatch tag.
The following sections show an example of throwing and handling a Java exception.
For more information on exception handling in ColdFusion, see Handling Errors.
Example: exception-throwing class
The following Java code defines the testException class that throws a sample exception. It also defines a myException class that extends the Java built-in Exception class and includes a method for getting an error message.
The myException class has the following code. It throws an exception with a message that is passed to it, or if no argument is passed, it throws a canned exception.
//class myException public class myException extends Exception { public myException(String msg) { super(msg); } public myException() { super("Error Message from myException"); } }
The testException class contains one method, doException, which throws a myException error with an error message, as follows:
public class testException { public testException () { } public void doException() throws myException { throw new myException("Throwing an exception from testException class"); } }
Example: CFML Java exception handling code
The following CFML code calls the testException class doException method. The cfcatch block handles the resulting exception.
<cfobject action=create type=java class=testException name=Obj> <cftry> <cfset Obj.doException() > <cfcatch type="myException"> <cfoutput> <br>The exception message is: #cfcatch.Message#<br> </cfoutput> </cfcatch> </cftry>