ArrayStoreException: bug or expected behavior?

Michael Hoffer info at michaelhoffer.de
Fri Jul 26 11:32:53 UTC 2019


A follow-up question: the following code automatically translates Point2D into Point2D?. Is this assumption correct? Does this mean that the reduction in required memory and the cache optimal layout are lost in this case?

        Point2DI[] points = new Point2DI[n];
        for(int i = 0; i < points.length;i++) {
            points[i] = new Point2D(i, i);
        }


Regards,
Michael

--
Dr. Michael Hoffer

Web:    mihosoft.eu
Twitter: @mihosoft

Goethe-Zentrum für Wissenschaftliches Rechnen (G-CSC)
Goethe-Universität
Kettenhofweg 139
60325 Frankfurt am Main
phone: +49 69 798 25254
info at michaelhoffer.de

> Am 26.07.2019 um 13:19 schrieb Michael Hoffer <info at michaelhoffer.de>:
> 
> Hi Tobias,
> 
> thanks for your quick response. You are right. I was too focused on inline types and didn’t see what actually caused the issue. So the root of the problem is that Java treats a Point2D[] array as a subtype of Point2DI[]. But that’s just wrong. This is indeed known and therefore expected behavior. Still feels broken though.
> 
> Regards,
> Michael
> 
> --
> Dr. Michael Hoffer
> 
> Web:    mihosoft.eu
> Twitter: @mihosoft
> 
> Goethe-Zentrum für Wissenschaftliches Rechnen (G-CSC)
> Goethe-Universität
> Kettenhofweg 139
> 60325 Frankfurt am Main
> phone: +49 69 798 25254
> info at michaelhoffer.de
> 
>> Am 26.07.2019 um 13:06 schrieb Tobias Hartmann <tobias.hartmann at oracle.com>:
>> 
>> Hi Michael,
>> 
>> thanks for trying out inline types!
>> 
>> I just had a quick look at this but when removing the "inline" from Point2D and executing with
>> latest Java, you get an ArrayStoreException exception as well (and no compile-time warning/error).
>> 
>> So if I don't miss anything, this isn't specific to inline types and therefore expected behavior.
>> 
>> Best regards,
>> Tobias
>> 
>> On 26.07.19 12:36, Michael Hoffer wrote:
>>> Hi all,
>>> 
>>> I finally found the time to play with the newest Valhalla build. We have a lot of use cases for this in the scientific community. Thanks for working on it.
>>> 
>>> I prepared a small test program that throws an ArrayStoreException during the assignment of an "inline value" to an array. I wanted to test the behavior of casting between "inline" and the nullable "?" type and interfaces. 
>>> 
>>> My expectations are that such errors should be prevented by the type system and should be discovered at compile-time. Either Point2D shouldn’t be allowed to implement the Point2DI interface (hopefully, that’s not the case) or the assignment of the points-array with an array of "inline values“ should be prevented (which is better).
>>> 
>>> Is this a bug or the expected behavior of the preview build?
>>> 
>>> Regards,
>>> Michael
>>> 
>>> --
>>> Dr. Michael Hoffer
>>> 
>>> Web:    mihosoft.eu
>>> Twitter: @mihosoft
>>> 
>>> Goethe-Zentrum für Wissenschaftliches Rechnen (G-CSC)
>>> Goethe-Universität
>>> Kettenhofweg 139
>>> 60325 Frankfurt am Main
>>> phone: +49 69 798 25254
>>> info at michaelhoffer.de
>>> 
>>> 
>>> /**
>>> * <p>
>>> * <b>How to reproduce the behavior/potential bug:</b>  
>>> * 
>>> * Compile this code with <b>jdk-14-valhalla+1-8 (2019/7/4)</b>:
>>> * 
>>> * <pre>
>>> * javac Main.java && java Main
>>> * </pre>
>>> * 
>>> * </p>
>>> * <p>
>>> * <b>Actual behavior:</b> a java.lang.ArrayStoreException is thrown
>>> * unexpectedly in line 33. 
>>> * </p>
>>> * <p>
>>> * <b>Expected behavior:</b> compile-time error that prevents the problematic
>>> * assignment in line 30.
>>> * </p>
>>> * 
>>> * @author Michael Hoffer <info at michaelhoffer.de>
>>> */
>>> public class Main {
>>>   public static void main(String[] args) {
>>> 
>>>       int n = 100_000_000;
>>> 
>>>       System.out.println("> creating " + n + " points");
>>> 
>>>       Point2DI[] points = new Point2D[n];
>>> 
>>>       for(int i = 0; i < points.length;i++) {
>>>           points[i] = new Point2DCls(i, i);
>>>       }
>>>   }
>>> 
>>>   static interface Point2DI {
>>>       double getX();
>>>       double getY();
>>>   }
>>> 
>>>   static inline class Point2D implements Point2DI {
>>>       public double x;
>>>       public double y;
>>> 
>>>       Point2D(double x, double y) {
>>>           this.x = x;
>>>           this.y = y;
>>>       }
>>> 
>>>       @Override
>>>       public double getX() {
>>>           return x;
>>>       }
>>> 
>>>       @Override
>>>       public double getY() {
>>>           return y;
>>>       }
>>>   }
>>> 
>>>   static class Point2DCls implements Point2DI {
>>>       public double x;
>>>       public double y;
>>> 
>>>       Point2DCls(double x, double y) {
>>>           this.x = x;
>>>           this.y = y;
>>>       }
>>> 
>>>       @Override
>>>       public double getX() {
>>>           return x;
>>>       }
>>> 
>>>       @Override
>>>       public double getY() {
>>>           return y;
>>>       }
>>>   }
>>> } // end class Main
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
> 



More information about the valhalla-dev mailing list