Arraylist statement with type or without

Asked

Viewed 229 times

6

I’d like to know the difference in declaring a ArrayList in this way:

List<BancoPerguntas> listaBancoPerguntas = new ArrayList<BancoPerguntas>();

List<BancoPerguntas> listaBancoPerguntas = new ArrayList<>();

In the second example do not pass the entity in the ArrayList.

Is there any difference?

  • 3

    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.

  • 1

    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.

  • 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

2 answers

7

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 :)

6

The two codes are equivalent. The second form with the diamond (<>) was introduced in Java 7 with the aim of leaving the syntax of generic types leaner, avoiding the redundancy of having to declare the generic type both in the constructor invocation and in the type declaration.

When the compiler finds the diamond, he looks at the variable declaration to find out what should be put there. In this way, the compiler transforms the second form into the first.

There is also a third form introduced in Java 10:

var listaBancoPerguntas = new ArrayList<BancoPerguntas>();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.