bugGNU Octave - Bugs: bug #42701, java toString method returns char...


bug #42701: java toString method returns char instead of java.lang.String

Submitted by:  Carnë Draug <carandraug>
Submitted on:  Mon 07 Jul 2014 09:49:25 PM UTC  
Category:  Libraries Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Matlab Compatibility
Status:  Confirmed Assigned to:  None
Originator Name:  Open/Closed:  Open
Release:  dev Operating System:  GNU/Linux

Add a New Comment(Rich Markup)

You are not logged in

Please log in, so followups can be emailed to you.


( Jump to the original submission)

Thu 19 Jul 2018 05:08:10 AM UTC, comment #6:

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:

Those getData() and getDataAsInteger() methods return exactly equivalent Java objects: Integers containing the value 42. But they are converted differently.

> ...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.

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.

Andrew Janke <apjanke>
Thu 19 Jul 2018 04:43:26 AM UTC, comment #5:

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.

Andrew Janke <apjanke>
Thu 15 Dec 2016 06:06:42 PM UTC, comment #4:

Octave 4.2.0 still behaves the same (as reported for Octave 3.8.1) in this respect.

Hartmut <hardy>
Thu 10 Jul 2014 11:56:18 AM UTC, comment #3:

I just asked on IRC, and on Matlab R2014a happens the same:

Carnë Draug <carandraug>
Project Member
Thu 10 Jul 2014 11:13:02 AM UTC, comment #2:

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:

Carnë Draug <carandraug>
Project Member
Thu 10 Jul 2014 01:18:58 AM UTC, comment #1:

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

Mike Miller <mtmiller>
Project Administrator
Mon 07 Jul 2014 09:49:25 PM UTC, original submission:

Java objects have a "toString" which returns a java.lang.String object. However, calling this method via Octave returns an Octave char. Example:

while in Matlab:

Carnë Draug <carandraug>
Project Member


(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:

No files currently attached


Depends on the following items: None found

Items that depend on this one: None found


Carbon-Copy List
  • -email is unavailable- added by apjanke (Posted a comment)
  • -email is unavailable- added by hardy (Posted a comment)
  • -email is unavailable- added by mtmiller (Posted a comment)
  • -email is unavailable- added by carandraug (Submitted the item)

    Do you think this task is very important?
    If so, you can add your encouragement to it.
    This task has 0 encouragements so far.

    Only project members can vote.




    Follow 5 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2018-04-27 mtmiller StatusNeed Info => Confirmed
        Release3.8.1 => dev
    2014-07-10 mtmiller CategoryNone => Libraries
        Item GroupNone => Matlab Compatibility
        StatusNone => Need Info

    Back to the top

    Powered by Savane 3.3