The nsjava::cast Command


nsjava::cast signature javaObj

The nsjava::cast command converts a Java object from one type to another. The signature argument provides the name of the Java class that the Java object should be cast to and the javaObj is the handle to the Java object that will be cast. The javaObj argument must be a valid Java object handle and the signature argument must specify a Java class that the javaObj argument can be assigned to. Casting is done in a type safe way as defined by the Java Language Specification. If a cast would violate the rules defined in the Java Language Specification, an error will be raised. Casting also works on java array objects. Array objects can be cast to an array of a parent class type or to the generic type Object.


An instance of the Java class java.lang.String can be allocated and cast into an instance of the class java.lang.Object with the following code.
# Get an instance of a String object.
set string [nsjava::new String "Hi there"]

# Cast the String to an Object reference
set object [nsjava::cast Object $string]

# Cast the Object reference back up to a String reference
set string2 [nsjava::cast String $object]

Copyright © 1997-1998 Sun Microsystems, Inc.