从MATLAB传递到Java的奇怪类 [英] Strange classes passed from MATLAB to Java
问题描述
我有一些需要互相交谈的MATLAB代码和一些Java代码.我得到一个NoSuchMethodError
.当我将MATLAB双精度数组传递给接受double[]
参数的Java方法时.
I have some MATLAB code and some Java code that need to talk with each other. I was getting a NoSuchMethodError
. When I pass a MATLAB double array to a Java method that accepts double[]
argument.
所以我写了一个简单的"hello world"来获取传递给方法的对象的类
So I write a simple "hello world" to get the class of an object passed to the method
public void printArray(Object array) {
System.out.println(array.getClass());
System.out.println(array.getClass().getPackage());
}
从MATLAB调用此方法,得到以下有趣的输出:
Calling this method from MATLAB, I get this interesting output:
>> a.printArray(2)
class java.lang.Double
package java.lang
>> a.printArray('hello')
class java.lang.String
package java.lang
>> a.printArray(true)
class java.lang.Boolean
package java.lang
>> a.printArray([2 3 4])
class [D
null
>> a.printArray([true false])
class [Z
null
有人可以解释发生了什么.我有MATLAB R14,并且Java类的编译兼容性为1.3.
Can someone explain whats happening. I have MATLAB R14 and the Java class is compiled with 1.3 compatibility.
推荐答案
I think the original problem has been updated by the OP, so I'll take the chance to summarize our findings so far:
-
我们已经确定原始问题中的示例代码会产生预期的行为. MATLAB将数据作为原语传递给Java,并且Java对对象执行适当的自动装箱.如logical按优先级匹配
boolean
,byte
,short
,int
,long
,float
和double
.
We have established that the sample code in the original question produces the expected behavior. MATLAB passes data as primitives to Java, and Java performs the appropriate autoboxing to Objects. As pointed out in Matthew Simoneau's reply, MATLAB explains how it matches its data types to Java data types in the "Passing Data to a Java Method" section of its documentation. The surprising thing is that a single MATLAB data type may match different Java data types, e.g.
logical
matchesboolean
,byte
,short
,int
,long
,float
, anddouble
, in that order of precedence.
OP最初遇到的NoSuchMethodError
是由使用错误的方法引起的.这是不再是问题. 使用double[]
作为方法参数有效.
The NoSuchMethodError
that the OP initially encountered was caused by the use of a wrong method. This is no longer a problem. Using double[]
as the method argument works.
奇怪的"类名称([D
和[Z
)实际上是Java用于描述基本类型数组的符号. API解释了 Class.getName().
The "strange" class names ([D
and [Z
) are actually notations used by Java to describe arrays of primitive types. The API explains the usage in Class.getName().
关闭案例=)
这篇关于从MATLAB传递到Java的奇怪类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!