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

 
 

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

Submitter:  Carnë Draug <carandraug>
Submitted:  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
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

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:


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.

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:


>> x = javaObject('java.lang.Double', 10);
>> y = x.toString();
>> class(y)

ans =

java.lang.String


Carnë Draug <carandraug>
Group 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:


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


Carnë Draug <carandraug>
Group 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


x = javaObject('java.lang.Double', 10);
y = x.toString();
class(y)


Mike Miller <mtmiller>
Group Member
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:


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


Carnë Draug <carandraug>
Group Member

 

(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

 

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)
  •  

    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 group 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.13-4448.
    Corresponding source code