For the complete experience, please enable JavaScript in your browser. Thank you!

  • Creative Cloud
  • Photoshop
  • Illustrator
  • InDesign
  • Premiere Pro
  • After Effects
  • Lightroom
  • See all
  • See plans for: businesses photographers students
  • Document Cloud
  • Acrobat DC
  • eSign
  • Stock
  • Elements
  • Marketing Cloud
  • Analytics
  • Audience Manager
  • Campaign
  • Experience Manager
  • Media Optimizer
  • Target
  • See all
  • Acrobat Reader DC
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player
  • All products
  • Creative Cloud
  • Individuals
  • Photographers
  • Students and Teachers
  • Business
  • Schools and Universities
  • Marketing Cloud
  • Document Cloud
  • Stock
  • Elements
  • All products
  • Get Support
    Find answers quickly. Contact us if you need to.
    Start now >
  • Learn the apps
    Get started or learn new ways to work.
    Learn now >
  • Ask the community
    Post questions and get answers from experts.
    Start now >
    • About Us
    • Careers At Adobe
    • Investor Relations
    • Privacy  |  Security
    • Corporate Responsibility
    • Customer Showcase
    • Events
    • Contact Us
News
    • 3/22/2016
      Adobe Summit 2016: Are You An Experience Business?
    • 3/22/2016
      Adobe Announces Cross-Device Co-op to Enable People-Based Marketing
    • 3/22/2016
      Adobe and comScore Advance Digital TV and Ad Measurement
    • 3/22/2016
      Adobe Marketing Cloud Redefines TV Experience
Functions in-k / 

JavaCast

Adobe Community Help


Applies to

  • ColdFusion

Contact support

 
By clicking Submit, you accept the Adobe Terms of Use.
 

Description

Converts the data type of a ColdFusion variable to a specified Java type to pass as an argument to Java or .NET object. Use only for scalar, string, and array arguments.

Returns

The variable, as type type.

Category

String functions

Syntax

JavaCast(type, variable)

History

ColdFusion MX 8: Added support for bigdecimal, byte, char, and short data types and for casting Arrays.ColdFusion MX 7: Added support for nulls.

See also

CreateObject, cfobject, Converting between .NET and ColdFusion data types in Using .NET classes and Java and ColdFusion data type conversions in Using Java objects in the Developing ColdFusion Applications

Parameters

Parameter

Description

type

Data type to which to convert variable:

  • bigdecimal (converts to java.math.BigDecimal)
  • boolean
  • byte
  • char
  • int
  • long
  • float
  • double
  • short
  • string
  • null
  • xxx where xxxis one of the following:
    • any of the preceding types, except for null
    • a Java class name

variable

A ColdFusion variable that holds a scalar or string type. Must be "" if type is null.

Usage

Use this method to specify the Java type to use for a variable that you use when calling a Java or .NET method when the conversion between types is ambiguous; for example, if a method is overloaded and differs only in parameter type or a .NET method is declared as taking a System.Object class parameter.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, 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. Because there is not a one-to-one correspondence between internally stored ColdFusion types and Java scalar types, some conversions cannot be performed. Use the result of this function only on calls to Java or .NET objects. The following example shows the use when calling a Java method.

<cfscript> 
x = CreateObject("java", "test.Hello"); 
x.init(); 
ret = x.sayHello(JavaCast("null", "")); 
</cfscript>

Note:

Do not assign the results of JavaCast("null","") to a ColdFusion variable. Unexpected results will occur.

The format JavaCast(type[], variable) casts a ColdFusion Array variable to a single dimensional Array of the specified type. It cannot convert multi-dimensional arrays. You can specify a primitive type or the name of a Class as the type to cast to. For example, you can use the following format to cast a ColdFusion Array to an Array of vom.x.yMyClass objects.

javacast("vom.x.y.MyClass[]", myCFArr)

Use an array in the first JavaCast parameter in any of the following circumstances:

 

  • You have two functions with signatures with the same number of parameters, and a parameter takes different types of Arrays in different signatures; for example, if you have both of the following functions: foo(int[] x) and foo(String[] strs).
  • The method parameter requires a class array in its signature; for example, foo(com.x.y.MyClass[]).
  • The method parameter requires an Object in its signature and you must pass an array of any particular type.The following example shows the use of the JavaCastfunction to cast arrays:You might have a fooClass class that defines the following two methods, each with two arguments where the first argument differs in the type of the array:

public class fooClass { 
public fooClass () { 
} 
public String foo(long[] arg) { 
return "Argument was a long array"; 
} 
public String foo(int[] arg) { 
return "Argument was an Integer array"; 
} 
}

To be able to use these functions in your CFML, use the JavaCastfunction to convert the ColdFusion Arrray to the array type required by one of the functions, as shown in the following code snippet:

<cfset arr = [1,2,4,20,10]> 
<cfset fooObj = createObject("java", "fooClass")> 

<cfset fooObj.foo(javacasr("int[]", arr))> 
<cfset fooObj.foo(javacast("long[]", arr))>

Example

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>

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy

Choose your region United States (Change)   Products   Downloads   Learn & Support   Company
Choose your region Close

Americas

Europe, Middle East and Africa

Asia Pacific

  • Brasil
  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Cyprus - English
  • Česká republika
  • Danmark
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Greece - English
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • Malta - English
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • Southeast Asia (Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam) - English
  • 台灣

Commonwealth of Independent States

  • Includes Armenia, Azerbaijan, Belarus, Georgia, Moldova, Kazakhstan, Kyrgyzstan, Tajikistan, Turkmenistan, Ukraine, Uzbekistan

Copyright © 2016 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy | Cookies

AdChoices