[lworld] What is the default of a Primitive.ref variable?

Srikanth Adayapalam srikanth.adayapalam at oracle.com
Fri Jul 23 11:47:48 UTC 2021


You could have asked the compiler ? 🙂

Prim.ref.default would have the same value and type that an uninitialized field of type Prim.ref would have by default.
Alternately if you do new Prim.ref[10]  the cells of the newly constructed array would have same value and type as
that of Prim.ref.default - so null.

As a result, the program you cite, should fail with NPE and it does on branch tip.

Srikanth
________________________________
From: valhalla-dev <valhalla-dev-retn at openjdk.java.net> on behalf of Jesper Steen Møller <jesper at selskabet.org>
Sent: 23 July 2021 16:17
To: valhalla-dev <valhalla-dev at openjdk.java.net>
Subject: [lworld] What is the default of a Primitive.ref variable?

Hi list

While making a test case for bug 8210906 (making SomeClass.default a poly-expression if SomeClass is parameterized), I came across the use of .default on a reference, as in

primitive class Prim {
    int i = 42;

    public static void main(String [] args) {
        Prim.ref p = Prim.ref.default; // Does this make sense at all?
        System.out.println(p.i); // Should this NPE or print 0?
    }
}

Prim.ref is the reference projection of Prim, and is nullable, and is reference type. The default of a reference is null.
But what is Prim.ref.default? Is it even a thing? Should it be disallowed?

-Jesper


More information about the valhalla-dev mailing list