Functions - java

cast

Returns an JObject|error, which is obtained after casting the provided JObject instance to the given JObject type depending on assignability.

createNull

Returns a handle, which refers to Java null.

fromString

Returns a handle, which refers to the Java string representation of the Ballerina string.

getClass

Returns a handle, which refers to the Java Class object associated with the class or interface with the given string name.

isNull

Returns true if this handle refers to Java null.

jObjToString

Returns the string representation of a Java object stored in a handle reference.

toString

Returns a Ballerina string representation of the Java object referred by the handle.

cast

(JObject value, typedesc castType)

returns JObject | error

Returns an JObject|error, which is obtained after casting the provided JObject instance to the given JObject type depending on assignability.

 JObject|error obj = java:cast(inputStream, typedesc<FileInputStream>);
 if (obj is JObject) {
   FileInputStream fileInputStream = <FileInputStream>obj;
 }

Parameters

  • value JObject
  • The JObject instance which is to be casted

  • castType typedesc
  • The typedesc<JObject> to which the given object is casted to if assignable

  • Return Type

    (JObject | error)
  • The JObject|error, which refers to the new JObject instance or an error

createNull

()

returns handle

Returns a handle, which refers to Java null.

 handle nullHandle = java:createNull();
  • Return Type

    (handle)
  • The handle, which refers to Java null

fromString

(string value)

returns handle

Returns a handle, which refers to the Java string representation of the Ballerina string.

 handle header = java:fromString("Content-Type");

Parameters

  • value string
  • The Ballerina string with which the handle is created

  • Return Type

    (handle)
  • The handle, which refers to the Java String representation of the Ballerina string

getClass

(string name)

returns handle | error

Returns a handle, which refers to the Java Class object associated with the class or interface with the given string name.

 handle|error intClass = java:getClass("int");

This function performs a Java class.forName(name) except for the following cases:

NameOutput
booleanJava Class instance representing the primitive type boolean
byteJava Class instance representing the primitive type byte
charJava Class instance representing the primitive type char
shortJava Class instance representing the primitive type short
intJava Class instance representing the primitive type int
longJava Class instance representing the primitive type long
floatJava Class instance representing the primitive type float
doubleJava Class instance representing the primitive type double

Parameters

  • name string
  • The name of the Java class

  • Return Type

    (handle | error)
  • The Java Class object for the class with the given name

isNull

(handle value)

returns boolean

Returns true if this handle refers to Java null.

 boolean status = java:isNull(value);

Parameters

  • value handle
  • The handle of which the referred value is to be tested with Java null

  • Return Type

    (boolean)
  • true if this handle refers to Java null

jObjToString

(handle jObj)

returns string

Returns the string representation of a Java object stored in a handle reference.

Parameters

  • jObj handle
  • The handle reference to the corresponding Java object.

  • Return Type

    (string)
  • The string representation of the Java object.

toString

(handle value)

returns string?

Returns a Ballerina string representation of the Java object referred by the handle. If the handle refers to Java null, then this function returns a nil value.

 string? version = java:toString(versionProperty);

Parameters

  • value handle
  • The handle of which the referred value is to be converted to a Ballerina string

  • Return Type

    (string?)
  • The Ballerina string representation of the Java object referred by the handle or else returns () if the handle refers to Java null