This is documentation for Mathematica 5, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)

Documentation / Mathematica / Add-ons & Links / J/Link / Part 1. Installable Java: Calling Java from Mathematica /

1.1.4 Conversion of Types Between Java and Mathematica

Before we encounter the operations of creating Java objects and calling methods, we should examine the mapping of types between Mathematica and Java. When a Java method returns a result to Mathematica, the result is automatically converted into a Mathematica expression. For example, Java integer types (e.g., byte, short, int, and so on), are converted into Mathematica integers, and Java real number types (float, double) are converted into Mathematica reals. The table below shows the complete set of conversions. These conversions work both ways—for example, when a Mathematica integer is sent to a Java method that requires a byte value, the integer is automatically converted to a Java byte.

Corresponding types in Java and Mathematica.

Java arrays are mapped to Mathematica lists of the appropriate depth. Thus, when you call a method that takes a double[], you might pass it {1.0, 2.0, N[Pi], 1.23}. Similarly, a method that returns a two-deep array of integers (i.e., int[][]) might return to Mathematica the expression {{1, 2, 3}, {5, 3, 1}}.

In most cases, J/Link will let you supply a Mathematica integer to a method that is typed to take a real type (float or double). Similarly, a method that takes a double[] could be passed a list of mixed integers and reals. The only times when you cannot do this are the rare cases where a method has two signatures that differ only in a real versus integer type at the same argument slot. For example, consider a class with these methods:

public void foo(byte b, Object obj);

public void foo(float f, Object obj);

public void bar(float f, Object obj);

J/Link would create two Mathematica definitions for the method foo—one that required an integer for the first argument and invoked the first signature, and one that required a real number for the first argument and invoked the second signature. The definition created for the method bar would accept an integer or a real for the first argument. In other words, J/Link will automatically convert integers to reals, except in cases where such conversion makes it ambiguous as to which signature of a given method to invoke. This is not strictly true, though, as J/Link does not try as hard as it possibly could to determine whether real versus integer ambiguity is a problem at every argument position. The presence of ambiguity at one position will cause J/Link to give up and require exact type matching at all argument positions. This is starting to sound confusing, but you will find that in most cases J/Link allows you to pass integers or lists with integers to methods that take reals or arrays of reals, respectively, as arguments. In cases where it does not, the call will fail with an error message, and you will have to use Mathematica's N function to convert all integers to reals explicitly.