The java::cast Command


java::cast signature javaObj

The java::cast command converts a Java object from one type to another. The signature argument provides the name of the Java class that the javaObj will be cast to. The javaObj argument must be a valid Java object handle and the signature argument must specify an accessible 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 [java::new String "Hi there"]

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

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

Copyright © 1997-1998 Sun Microsystems, Inc.