small ideas to improve java

Ahmed Ahmed ah_mz_el2003 at
Mon Aug 15 21:36:21 UTC 2016

Hi dalibor topic

On 12.08.2016 09:44, Dalibor topic wrote:

> On 11.08.2016 23:05, Ahmed Ahmed wrote:
>> 1) my first idea about the restriction that the call of super() must be the first statement in the child constructor

>Please see

>> 2) If arrays of parameterized type were allowed

> Please see

i have read the article also the a go through State of the Specialization and also "Model 2" prototype status i didn't understand most of the subject and it denoted to me that i'm too far from discussing language specification and i was hesitated to annoy you guys again by me as a literate programmer but i decided to post the replay any way 

regarding the two prototypes it isn't clear to me if parameterized type information will be 
available at run time or not meaning would Box<String> and Box<Integer> represented at run 
time as Box<String> and Box<Integer> by some mean or both of them will converge to Box<Object> 
at run time or will they remain just a Box 

what i understand from generic specialization is that all instantiations of parameterized types by reference parameter type will continue to be represented as erased raw type and only instantiation by primitive type will have actual special representation at run time ? or was i wrong 

in any case suppose that parameter information will be preserved at run time and Box<int>, Box<float>, Box<String> and Box<Integer> will be considered different what is the effect of this in creating array of parameterized type like this 

Box<? extends Number>[] nums = new Box<>[10]; 

will the array type be Box[] or Box<Number> or Box<? extends Number>[] at run time ?

also again consider this example 

class Box<T> {

    T item;

    public Box(T item) {
        this.item = item;

    public static void main(String args[]) {
        Box<Integer>[] ints= new Box<>[10];//not allowed yet
                                           //but in current java 8 i can write = new Box[10] 
                                           //and it is ok ? the compiler complain that it
                                           //is unchecked but really that is not the point
                                           //at which the problem occurs
        ints[0] = new Box<>(10);//ok
        Box[] anys = ints;//also Box<?>[] anys//ok and this what i think is the problem 
        anys[1] = new Box<String>("hi i'm string");//should fail but not and if parameter 
                                                   //information for reference type will be
                                                   //erased to raw type in the new generics 
                                                   //we will still be in the same place
        ints[1].item += 10;//fail by class cast exception 


no i wonder will parameterized type arrays creation be allowed or not 
if yes what will be the decision about this arrays will it be covariant or will it be nonvariant 

also it appear to me that currently arrays of parameterized type are nonvariant e.g
Box<Integer>[] ints = new Box[10];
Box<Number>[] nums = ints;//not allowed

as in contrast to usual arrays 

Number[] ns = new Integer[10];
Box<? extends Number>[] nums = ints;//allowed ? really getting messy 

if type parameter information are available at run time what we will gain is array store exception "at run time" instead of class cast exception "at run time" few steps later, is it a significant gain ? ok to some extent but wouldn't it be nice if the compiler complained about it before going to the run time ?

so my idea basically to make arrays of parameterized type have some sort of non-variance "and as it appear are partially done" by just raising warning if trying to assigning an array of more specialized upper bound to one with less specialized upper bound with this rule the following should be safe "i assume!" and the compiler shall be satisfied 
Box<Number>[] nums = new Box[10];
Box<? extends Number>[] anyNum = nums;//the upper bound of anyNum is not less specific 
                                      //than nums upper bound 
anyNum[0] = new Box<Integer>(1);//ok
anyNum[1] = new Box<Double>(1.0);//ok

but this indeed unsafe and the compiler should not allow/warn us about it 

Box<? extends Object>[] objs = anyNum ;
this will give us safety over arrays also will allow us to make arrays of parameterized type "we already can make it as i pointed in the examples" more safely and i assume it is easy task that can be added to java 9 rather than waiting another few years till java 10 or 11 

More information about the adoption-discuss mailing list