# Converting Java Types in Script with Variant

ECMAScript provides a Variant type that you can use to convert and specify Java types in script. In script, this type is typically at work invisibly.

This is particularly useful in cases where your script code makes a call to an overloaded method of a Java class. Unlike Java, ECMAScript is a dynamically typed language. This means that you don't specify a type when you declare script variables, allowing the script interpreter to specify the type at run time.

You can specify particular Java types in script by using a casting syntax. At run time, the interpreter converts variables and values to the Java types you specify. For example, the Java Math class provides four max methods that return the larger of their two arguments. Each max method takes number pairs of different types: double, float, int, or long. In script, to specify the method that takes two int values, you could write the following:

    var greaterNumber = java.lang.Math.max((int)200, (int)100);

When you use casting in script to convert a type, the resulting type is a Variant that is automatically converted when it's used. In other words, the following two lines are equivalent:

    x = (int)10;
    x = new Variant(10).changeType(Variant.INTEGER_TYPE);

One of the most useful conversions supported by Variant is between date formats. For example, the following conversions are all supported because Variant is the underlying type when you are casting in script.

    javaDate = new java.util.Date(currentMilliSeconds);
    // Convert to the XmlDate type provided with XMLBeans.
    xmlDate = (XmlDate)javaDate;
    // Convert to an SQL Date.
    sqlDate = (java.sql.Date)javaDate;

You can get the Java type by using the Variant class's getTypeName method, as shown here:

    var someDouble = (double)10.0;

    // Print the type's name: "double"
    print(someDouble.getTypeName());

## Methods of the Variant Type

The following lists the methods exposed by Variant instances you create. In keeping with the Variant type's role in helping with conversion, these methods let you convert from one type to another, and to determine what type the Variant variable currently is.

    public Object getValue ();

    public String toString ();

### Methods for Discovering the Type

    public int getType ();

- Returns one of the constants described later in this topic.

    public boolean isUndefined ();

    public boolean isNull ();

    public boolean isEmpty ();

- Returns true if this type is null or undefined.

    public boolean isBoolean ();

    public boolean isAnyNumber ();

    public boolean isAnyDate ();

    public boolean isAnyTime ();

    public boolean isPrimitive ();

- Returns true if this type is a boolean, byte, short, int, long, double, real, or char

    public boolean isAnyInteger ();

- Returns true if this type is a byte, short, int, or long

    public boolean isAnyFloatingPoint ();

- Retuns true if this type is a double or real

    public boolean isAnyString ();

- Returns true if this type is a char or string

### Methods for Converting from One Type to Another

#### Changing the Current Variable's Type

    public void changeType (int type) throws ClassNotFoundException;

- The type parameter should be one of the constants described later in this topic. For example, the following lines declare a String variable, then change its type to an int (with a value of 12345):

      x = (String)"12345";
      y = x.changeType(Variant.INTEGER_TYPE);

    public void changeType (Class newClass) throws ClassNotFoundException;

    public void changeType (String newClass) throws ClassNotFoundException;

#### Getting Another Type from the Current Variable

    public Boolean getAsBoolean ();

    public Byte getAsByte ();

    public Character getAsCharacter ();

    public Double getAsDouble ();

    public Float getAsFloat ();

    public Integer getAsInteger ();

    public Long getAsLong ();

    public Number getAsNumber ();

    public Object getAsObject ();

    public Short getAsShort ();

    public String getAsString ();

    public Object getAsArray ();

    public java.util.Date getAsJavaDate ();

    public IJsxObject getAsJsxDate ();

    public XmlDate getAsXmlDate ();

    public java.sql.Date getAsSQLDate ();

    public XmlDateTime getAsXmlDateTime ();

    public java.sql.Time getAsSQLTime ();

    public IJsxObject getAsJsxObject ();

#### Setting the Current Variable to a New Type and Value

    public void setBoolean (Boolean value);

    public void setByte (Byte value);

    public void setCharacter (Character value);

    public void setDouble (Double value);

    public void setFloat (Float value);

    public void setInteger (Integer value);

    public void setLong (Long value);

    public void setNumber (Number value);

    public void setObject (Object value);

    public void setShort (Short value);

    public void setString (String value);

    public void setArray (Object value);

    public void setJsxObject (Scriptable value);

    public void setJavaDate (java.util.Date value);

    public void setJsxDate (IJsxObject value);

    public void setXmlDate (XmlDate value);

    public void setSQLDate (java.sql.Date value);

    public void setXmlDateTime (XmlDateTime value);

    public void setSQLTime (java.sql.Time value);

### Constants Representing Specific Types

The following constant represent specific types handled by Variant. When you call the getType method to discover a variable's current type, the method returns the int value associated with one of these constants. Likewise, when calling the changeType(int) method, you pass one of these as the parameter to indicate which type to change the variable to.

    UNDEFINED_TYPE = 0;

    NULL_VALUE = 1;

    BOOLEAN_TYPE = 2;

    BYTE_TYPE = 3;

    HARACTER_TYPE = 4;

    DOUBLE_TYPE = 5;

    FLOAT_TYPE = 6;

    INTEGER_TYPE = 7;

    LONG_TYPE = 8;

    NUMBER_TYPE = 9;

    OBJECT_TYPE = 10;

    SHORT_TYPE = 11;

    STRING_TYPE = 12;

    ARRAY_TYPE = 13;

    JAVADATE_TYPE = 14;

    JSXDATE_TYPE = 15;

    SQLDATE_TYPE = 16;

    XMLDATE_TYPE = 17;

    JSXOBJECT_TYPE = 18;

    XMLOBJECT_ANY = 19;

- Represents untyped XML

    XMLOBJECT_COMPLEX = 20;

- Represents a schema (XSD) complex type

    SQLTIME_TYPE = 21;

    XMLDATETIME_TYPE = 22;

Related Topics

None.