It’s been said it’s inference and I think you can infer (Pun intended) in the comments that is just a syntactic difference without affecting semantics, performance or any other issue.
What you’re doing in the second example is just omitting something the compiler already knows, it’s just not written down, but the information is still there.
In Java 10 it is possible to do different in local variables:
var listaBancoPerguntas = new ArrayList<BancoPerguntas>();
Again the var
changes nothing, the compiler reads what is on the right side of the expression and sees that makes an assignment and through it the type can be determined even without being explicitly written in the place it should. Think about it, so you can type the same thing twice?
It is also possible to do:
var x = 1;
and
var y = retornaUmInt();
I put in the Github for future reference.
But it is not customary to recommend this last case because it is less readable to know which type is the variable.
There are cases that it is not possible to use the inference, because the type of the variable needs to be different from the type of the constructor/literal.
It is very complicated to do the same for instance variables, so it is not allowed.
Java is improving :)
This is called Inference of type. only declaring the type on the left side, java automatically considers the diamond operator empty(<>)on the right side with the same type, leaving the declaration shorter and succinct.
– user28595
Type inference has been introduced in Java 7. The difference beyond the statement getting shorter is that from this version if you do not use inference the compiler starts to charge a warning (Warning), which does not get to prevent the compilation.
– Piovezan
Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site
– Maniero