View comments | RSS feed

JavaCast

Description

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.

Return value

The variable, as type type.

Category

String functions

Syntax

JavaCast(type, variable)

See also

CreateObject, cfobject

Parameters

Parameter Description
type
Data type to which to convert variable:
  • boolean
  • int
  • long
  • double
  • string
variable
A ColdFusion variable that holds a scalar or string type

Usage

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.

Example

The fooClass method fooMethod takes one overloaded argument, 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>

ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6

Comments are no longer accepted for ColdFusion MX. ColdFusion 8 is the current version.

Comments


hlichtin said on Mar 19, 2003 at 4:05 PM :
JavaCast also takes float as a type.
Alexander Bunkenburg said on Sep 21, 2004 at 4:12 AM :
JavaCast is used to distinguish overloaded methods in calls from CF to Java. I propose a gentle improvement to CF: How about if CF first tried to choose the right method based on number of parametres? The CF programmer then only needs to give hints using JavaCast if there are multiple overloaded methods with the same number of parametres.
The Java language spec does something similar.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6/CFML_Reference/functions-pt1147.htm