JavaCast

Converts the data type of a ColdFusion variable to pass as an argument to an overloaded method of a Java object. Use only for scalar and string arguments.

The variable, as type type.

String functions

JavaCast(type, variable)

CreateObject, cfobject

Parameter Description

type

Data type to which to convert variable:

  • boolean
  • int
  • long
  • float
  • double
  • string

variable

A ColdFusion variable that holds a scalar or string type

Use after creating a Java object with the cfobject tag, before calling one of its methods. If the method takes more than one overloaded argument, you must call JavaCast for each one. Use JavaCast 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, nor to cast to a super-class. Use this function's result only on calls to Java objects. Because there is not a one-to-one correspondence between internally stored ColdFusion types and Java scalar types, some conversions cannot be performed.

The method fooMethod in the class fooClass takes one overloaded argument. The fooClass class is defined as follows:

public class fooClass {
   public fooClass () {
   }
   public String fooMethod(String arg) {
      return "Argument was a String";
   }
   public String fooMethod(int arg) {
      return "Argument was an Integer";
   }

}

Within ColdFusion, you use the following code:

<cfobject
action="create"
    type = "java"
    class = "fooClass" 
    name = obj>

<!--- ColdFusion can treat this as a string or a real number --->
<cfset x = 33>

Perform an explicit cast to an int and call fooMethod:<br>
<cfset myInt = JavaCast("int", x)> 
<cfoutput>#obj.fooMethod(myInt)#</cfoutput>
<br><br>
Perform an explicit cast to a string and call fooMethod:<br>
<cfset myString = javaCast("String", x)> 
<cfoutput>#obj.fooMethod(myString)#</cfoutput>

View comments on LiveDocs