Quelle est la différence entre new ArrayList<>(); et New ArrayList<String>(); ?
goune stephie
Niveau 22
ArrayList<>();
Discussion en cours
Commentaires (4)
- Populaires
- Nouveau
- Anciennes
Tu dois être connecté(e) pour laisser un commentaire
Thomas
4 juillet 2021, 07:25
It's called type inference and is there to save you typing work and to make the code look cleaner.
Or did you want to ask why there is the diamond operator at all? Then the keyword is type safety. Without specifying the question there is a lot to answer ;)
0
Gellert Varga
4 juillet 2021, 22:38
I would like to ask if there is a difference between the meaning of the following two lines of code, and is there any significance to the difference?
0
Thomas
5 juillet 2021, 05:40
Your second list declaration is valid from Java 7 on (called diamond syntax cause of <>) and one of the smaller changes introduced with type inference.
You see that all necessary information is available on the left hand side of the declaration. So compiler writers don't force us anymore to repeat that information on the right hand side. The compiler itself determins the type.
So both of the examples are equivalent and result in the same bytecode.
+2
Gellert Varga
5 juillet 2021, 08:59
OK, thank you!:)
0