Indicates the data type conversion of a ColdFusion variable to pass as an argument to an overloaded method of a Java object. It should be used only for scalar and string arguments.
JavaCast(type, variable)
You use JavaCast
after creating a Java object with cfobject, before calling one of its methods. If the method takes more than one overloaded argument, then you must call JavaCast
for each overloaded argument. JavaCast
should be used only when a method is overloaded, because its arguments can take more than one data type, not because the method can take a variable number of arguments.
JavaCast
cannot be used to cast between complex objects, or to cast to a super-class. The result of this function should be used only on calls to Java objects. Because there is no one-to-one correspondence between internally stored ColdFusion types and Java scalar types, some conversions cannot be performed.
Consider that fooClass has a method fooMethod that takes a single argument, which is overloaded as follows:
public void fooMethod(String arg);
public void fooMethod(int arg);
Within ColdFusion, you use the following code:
<cfobject type = java CLASS = fooClass name = obj>
<!--- ColdFusion may treat this as a string or a real number ---> <cfset x = 33> <!--- Perform an explicit cast to an int. ---> <cfset myInt = JavaCast("int", x)> <cfset void = fooMethod(myInt)> <!--- Perform an explicit cast to a string. ---> <cfset myString = javaCast("String", x)> <cfset void = fooMethod(myString)> </cfobject>