bugGNU Octave - Bugs: bug #48790, javaArray('double',1)

 
 

bug #48790: javaArray('double',1)

Submitted by:  Ernst Reissner <ernstreissner>
Submitted on:  Mon 15 Aug 2016 05:25:25 PM UTC  
 
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  None
Status:  Need Info Assigned to:  None
Originator Name:  Open/Closed:  Open
Release:  dev Operating System:  Any

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)

Sun 01 Jul 2018 05:11:39 AM UTC, comment #6: 

Hi folks.

There might be a bit of confusion here about the exact Java types we're dealing with.

In Java:

  • java.lang.Double is not the same as the primitive double type: it is an object wrapper for it
  • java.lang.Double.class is the class of the object wrapper
  • java.lang.Double.TYPE is the class of the corresponding primitive type

Matlab does not auto-convert the object wrapper types like java.lang.Double or java.lang.Double[]; it merely has a convenience display function that makes them look like primitive types.

>> java.lang.Double(42)
ans =
42.0
>> class(ans)
ans =
    'java.lang.Double'
>> java.lang.Double.class
ans =
    'java.lang.Double'
>> java.lang.Double.TYPE
ans =
double
>> class(ans)
ans =
    'java.lang.Class'

Andrew Janke <apjanke>
Fri 30 Mar 2018 11:51:49 AM UTC, comment #5: 

Hello, may I bump this issue ? In an application the vector handed to a java method occasionnaly contains only one single element, and currently there seems to be no way of providing a single-element-array (see #53524). If it were possible to construct and initialise a Java array of primitive type explicitely then one could work around that problem.

Adrian <adaerr>
Fri 19 Aug 2016 08:23:44 AM UTC, comment #4: 

Nonono!!!

The site you cite,
 https://www.mathworks.com/help/matlab/ref/javaarray.html,
clearly says

ObjArr = javaArray(PackageName.ClassName,x1,...,xN) constructs an empty Java® array object for objects of the specified PackageName.ClassName class.

That is clear and that is not what is done in Octave.
What you cite:

> To create an array of primitive Java types, create an array of the equivalent MATLAB type, shown in the Conversion of MATLAB Types to Java Types table. See Conversion of MATLAB Argument Data.


is a tip only and it says nothing about the return type of javaArray, right?
The tip is not bad, but not always applicable,
as seen in you other link,

https://www.mathworks.com/matlabcentral/answers/96483-how-can-i-pass-a-ragged-array-of-java-primitive-types-to-a-java-function-from-matlab

It is about ragged arrays, which are supported by Java but not by Octave.

By the way, although maybe

> The ability to create Java primitive types is not available in MATLAB.


Octave HAS the ability to create arrays of primitive types.
It is an undocumented feature, that instead of a string,
one can pass a class and in particular Double.TYPE
as I pointed out below.
Maybe matlab can do so as well.

Also:
javaObject('java.lang.Double',42)
 yields

ans =

<Java object: java.lang.Double>

not just 42 for that reason.

A re

Ernst Reissner <ernstreissner>
Fri 19 Aug 2016 01:41:19 AM UTC, comment #3: 

According to https://www.mathworks.com/matlabcentral/answers/96483-how-can-i-pass-a-ragged-array-of-java-primitive-types-to-a-java-function-from-matlab

> The ability to create Java primitive types is not available in MATLAB.


And according to https://www.mathworks.com/help/matlab/ref/javaarray.html

> To create an array of primitive Java types, create an array of the equivalent MATLAB type, shown in the Conversion of MATLAB Types to Java Types table. See Conversion of MATLAB Argument Data.


So there is no Matlab compatibility issue here, Matlab simply dodges the issue. I see nothing wrong with what Octave does with this. The normal return value conversion rules should apply.

If there is any bug here, I think it's this

>> arr = javaArray (__java_get__ ("java.lang.Double", "TYPE"), [3 3])
arr =

<Java object: double[][]>

>> size (arr)
ans =

   3   3

According to https://www.mathworks.com/help/matlab/matlab_external/handling-data-returned-from-a-java-method.html, any rectangular array of a primitive type returned by any function should be converted to the corresponding Octave type. So I would think arr above should be converted to an Octave double matrix of size [3, 3].

Mike Miller <mtmiller>
Project Administrator
Thu 18 Aug 2016 10:27:32 PM UTC, comment #2: 

The problem is, that javaArray is a constructor invocation in matlab which means that the result is a java array.
In the example I provided, the result is an octave array or even an octave scalar, because boxing is applied.
The latter is wrong.

Ernst Reissner <ernstreissner>
Thu 18 Aug 2016 05:46:51 PM UTC, comment #1: 

I don't see a bug here. Can you explain what you think should be returned instead? What does Matlab do? I can't find any reference on the Matlab docs of constructing an array using the javaArray funcion with the type set to a primitive (non-class) type.

But assuming the same return value conversion rules apply as with other functions, it looks to me like everything is working correctly here.

Mike Miller <mtmiller>
Project Administrator
Mon 15 Aug 2016 05:25:25 PM UTC, original submission:  

According to

http://de.mathworks.com/help/matlab/ref/javaarray.html?searchHighlight=javaArray

javaArray(cls,1) has to return a java array
i.e. for Octave an instance of octave_java
wrapping some new cls[1].

Consider
octave:16> dblCls=java_get('java.lang.Double', 'TYPE')
dblCls =

<Java object: java.lang.Class>

This is the class of the type double.

o=javaArray(dblCls,1)
 seems to be 0
and really:
class(o)
yields double instead of double[]

I know also the reason: javaArray is based on javaMethod
which performs boxing and unboxing.
That way, double[] is converted to a double scalar.

Ernst Reissner <ernstreissner>

 

(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 adaerr (Posted a comment)
  • -email is unavailable- added by ernstreissner
  • -email is unavailable- added by ernstreissner (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 4 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2019-02-22 mtmiller Carbon-CopyRemoved 80942 => -
    2016-08-18 mtmiller CategoryNone => Octave Function
        StatusNone => Need Info
    2016-08-15 ernstreissner Carbon-Copy- => Added rei3ner@arcor.de

    Back to the top


    Powered by Savane 3.6