Add a New Comment (Rich Markup)
( Jump to the original submission )
Wait, wait. I take that back. The documentation is correct, but you have to read and parse it very carefully. The doco starts off by saying: "When a Java method is declared to return data of type java.lang.Object, MATLAB converts its value depending on the actual type returned." This conversion only happens when exactly that situation is true: you call a method whose declared return type is exactly java.lang.Object, and not a subclass of Object. Consider this Java class:
package net.janklab.test; public class DemoType { public Object getData() { return new java.lang.Integer(42); } public Integer getDataAsInteger() { return new java.lang.Integer(42); } public Object getStringAsObject() { return "foo"; } public String getStringAsString() { return "foo"; } }
Those getData() and getDataAsInteger() methods return exactly equivalent Java objects: Integers containing the value 42. But they are converted differently.
>> obj = net.janklab.test.DemoType obj = net.janklab.test.DemoType@1fc0053e >> x = obj.getData x = 42 >> class(x) ans = 'double' >> y = obj.getDataAsInteger y = 42 >> class(y) ans = 'java.lang.Integer' >>
> ...says that a Java return value of java.lang.String should be auto-converted into a Matlab type char value...
So, yes, it will be auto-converted, but only if it comes out of a method declared to return exactly Object, and not one declared to return String, even though the run-time Java values are equivalent.
>> obj = net.janklab.test.DemoType obj = net.janklab.test.DemoType@172ca72b >> s1 = obj.getStringAsObject s1 = 'foo' >> class(s1) ans = 'char' >> s2 = obj.getStringAsString s2 = foo >> class(s2) ans = 'java.lang.String' >>
And so, since normal toString() methods are declared to return String and not Object, their return values are not subject to auto-conversion, and remain java.lang.String objects. If you ask me, this is wacky behavior, and no wonder we're all confused. I've been coding Java+Matlab for nearly 15 years now, and never understood this subtlety until now.
I think that whole "java.lang.Object Return Types" table in the linked Matlab documentation is incorrect, or grossly out of date. None of those object types get auto-converted by Matlab. And it contradicts the statement directly under the table: "There is no conversion if the return argument is a subclass of Object or an array of Object." That statement is true, and all of the listed object types are subclasses of Object. You're not misunderstanding it, Mike. The doco is wrong, IMHO.
Octave 4.2.0 still behaves the same (as reported for Octave 3.8.1) in this respect.
I just asked on IRC, and on Matlab R2014a happens the same:
>> x = javaObject('java.lang.Double', 10); >> y = x.toString(); >> class(y) ans = java.lang.String
I tested with Matlab R2010b. Yes, the documentation does suggest that but then, the following documentation on the same page:
> With the MATLAB char function, you can convert java.lang.String > objects and arrays to MATLAB character arrays.
does not make a lot of sense. Why are they writing about calling char() on a java.lang.String if that an object cannot exist (it is automatically converted to char)? This is very much against their documentation. I just tried your second example:
>> class (x.toString ()) >> x = javaObject('java.lang.Double', 10); >> y = x.toString(); >> class(y) ans = java.lang.String >> version ans = 7.11.0.584 (R2010b)
Hmm, this seems to directly contradict Matlab's own documentation. At http://www.mathworks.com/help/matlab/matlab_external/handling-data-returned-from-a-java-method.html#f61197 they explicitly say
> When a method call returns data of type java.lang.Object, > MATLAB converts its value, depending on its actual type, > according to the following table.
and says that a Java return value of java.lang.String should be auto-converted into a Matlab type char value. Unless I'm completely misunderstanding that page. What version of Matlab did you observe this in? Any different result with
x = javaObject('java.lang.Double', 10); y = x.toString(); class(y)
Java objects have a "toString" which returns a java.lang.String object. However, calling this method via Octave returns an Octave char. Example:
octave-cli-3.8.1> x = javaObject ('java.lang.Double', 10) x = <Java object: java.lang.Double> octave-cli-3.8.1> class (x.toString ()) ans = char
while in Matlab:
>> x = javaObject ('java.lang.Double', 10); >> class (x.toString ()) ans = java.lang.String
(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)
Attach Files: Comment:
No files currently attached
Depends on the following items: None found
Items that depend on this one: None found
There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.
Only project members can vote.
Please enter the title of George Orwell's famous dystopian book (it's a date):
Follow 5 latest changes.
Copyright © 2023 Free Software Foundation, Inc. Verbatim copying and distribution of this entire article is permitted in any medium, provided this notice is preserved. The Levitating, Meditating, Flute-playing Gnu logo is a GNU GPL'ed image provided by the Nevrax Design Team. Source Code
Powered by Savane 3.12