1

In Java 10, this is giving me a warning -

ArrayList a = new ArrayList<>();
a.add(23);
a.add("str");

"ArrayList is a raw type. References to generic type ArrayList should be parameterized"

And the reason is Generic behind, but for this code

var b = new ArrayList<>();//Object type
b.add(3);
b.add("string");

Why was any warning not given by compiler?

Note:- I know var is limited to method scope. Just wanted to know design concept resected to generics for var

Edit1:- Don't mark as duplicate as I just wanted to know about internal design and why java not added generic stuff for var?

Naman
  • 27,789
  • 26
  • 218
  • 353
Roushan
  • 4,074
  • 3
  • 21
  • 38
  • Open this in debugger and check what type of array list it is. It's Object. – agilob Apr 19 '18 at 19:29
  • i know its object my question is not this @agilob – Roushan Apr 19 '18 at 19:40
  • @shmosel the question you linked certainly not the answer full answer of my question, please read the full question before flagging it – Roushan Apr 19 '18 at 19:44
  • 4
    Your question doesn't make sense. Why should there be a warning? What's not typesafe about it? – shmosel Apr 19 '18 at 19:45
  • yes , before had the warning but why not for var – Roushan Apr 19 '18 at 19:48
  • 2
    The duplicate tells you everything you need to know about `var` and diamond notation. If you read it properly and are still confused, please update the question to clarify the problem. – shmosel Apr 19 '18 at 19:53
  • 4
    It doesn't give you a warning because the inferred type is `ArrayList`, not `ArrayList`. Again, this is all covered by the dupe. – shmosel Apr 19 '18 at 20:02
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/169372/discussion-between-roushan-and-shmosel). – Roushan Apr 19 '18 at 20:05
  • 1
    @shmosel I am not convinced this is a dupe. The question here is why is this raw type not getting a compile warning. – bhspencer Apr 19 '18 at 20:33
  • 5
    @bhspencer Because it's not a raw type. – shmosel Apr 19 '18 at 20:39
  • I second the thought by @shmosel, you can try and [compile this class](https://github.com/namannigam/jdk10-updates/blob/master/jdk-features/src/com/stackoverflow/nullpointer/lvti/Q49928780_RawTypeWarn.java) to confirm that `var b` is inferred as `ArrayList` and not `ArrayList` in which case you wouldn't get a warning. On the other note, if you are using intelliJ as an IDE, I guess they, on the contrary, align with you and are displaying the warning when I use `var b = new ArrayList<>(); b.add("1");` as well. – Naman Apr 20 '18 at 01:53
  • 1
    See also [this comment](https://stackoverflow.com/questions/48428434/java-10-will-java-7s-diamond-inference-work-with-local-type-inference#comment83914648_48429067) – Holger Apr 20 '18 at 11:35
  • 3
    I don't think its a duplicate, as much as just not understanding how type inference works. The OPs assumption that we'd infer a raw type here is simply wrong. (Briefly: the type on the RHS is `ArrayList`, where alpha is an inference variable; we would gather constraints on alpha from the arguments and target context, neither of which contribute anything; we therefore fall back on the promordial constraint `alpha <: Object`, and solve for alpha, yielding `ArrayList`.) We do not use other uses of `a` to gather additional constraints. – Brian Goetz Apr 20 '18 at 15:27
  • 1
    @BrianGoetz but the first answer of the linked question says “`var list = new ArrayList<>(); // Infers ArrayList`”. That’s precisely answering the OP’s question and the fact that the OP makes a fundamentally wrong assumption is not reason enough to keep this question open. – Holger Apr 20 '18 at 15:32
  • 2
    @Holger Meh. You can interpret "duplicate of question X" as "the answer to this question is buried in the answer to that question", if you want, but then don't be surprised that people don't agree, as that's not actually what the word "duplicate" means. This OP is not asking the same question as the other. (But then we could have just have two questions for type inference, one of which is "I don't understand how type inference works, help" and the other is "why did you design type inference the way you did", and close all the others as dups.) – Brian Goetz Apr 20 '18 at 17:04
  • 1
    @BrianGoetz note that the header says “*This question already has an answer here*: …”, though, admittedly, Stackoverflow sometimes uses the header “*This question is an exact duplicate of*: …” instead, apparently chosen by a dice roll. I don’t say that any question regarding the same topic should be closed as dupe, but here, we’re talking about a question that is based on a false premise that can be clarified with a single sentence: “*that variable does not have a raw type*”. If not closed as duplicate, it should have been closed for one of the other reasons. – Holger Apr 20 '18 at 17:13
  • @BrianGoetz it's reopened, you may post an answer now – Eugene Apr 23 '18 at 08:03

2 Answers2

12

Here's how the compiler computes the type of

var b = new ArrayList<>();

It starts by computing the standalone type of the initializer. Because the initializer is a diamond invocation, we must use inference. So we introduce an inference variable alpha, and the type on the RHS is ArrayList<alpha>. Now we must solve for alpha.

To solve, we gather constraints. We start with the primordial constrain that alpha <: Object (because all type variables are reference types). We then look to the constructor args (nothing there) and the target type (nothing there) to gather constraints. So the only constraint we have is alpha <: Object, and so we choose alpha=Object, and get ArrayList<Object> on the RHS. That's the type of b.

No raw type here, hence no warning.

Brian Goetz
  • 90,105
  • 23
  • 150
  • 161
0

Because it is an ArrayList<Object>. Object is the top level type in Java which represents any type, so it is always safe to add anything to the list.

In the first example, you got a warning because you used a raw type. Raw types are legacy in Java and only exist for backward compatibility. You should not use them.

ZhekaKozlov
  • 36,558
  • 20
  • 126
  • 155