Re: what is the initial value of arrays of object
Andreas Leitgeb wrote:
Zerg's proposal avoids verbose wording and yet keeps the full
initialisation together with the declaration and the constructor
(provided that indeed the thusly construed items are what is
wanted). It is far more concise than calling Array.fill for
primitives or looping for Objects.
I don't know, but I've always found the non-standard ways of
initialization rather... beautiful. For example, I have a Map initalized
as follows:
Map<String,String> map = new HashMap<String,PrimitiveClass>() {{
put("Key1", "Value1");
put("Key2", "Value2");
}};
Similarly with Lew's example of initializing a List.
The examples Patricia gave are much easier on my eyes, at least,
especially since the salient differences between null-initialization and
non-null initialization are more than a few characters.
PS: where I disagree to zerg is, that I'd actually *want* the
multiple-evaluation and making use of possible sideeffects:
List<String> list=getStringList(); // just for context
Iterator<String> it=list.iterator(); // just for context
MyObj moArr=new MyObj[list.size()](it.next());
I would agree that multiple-evaluation makes more sense as a use case,
BUT to do so would be to completely ignore Java's basic syntax style. So
multiple-evaluation is the ideal case but also a non-starter.
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth