-3

I've come across a question of best practice. Which option is better:

List list = new ArrayList<String>();

or

List<String> list = new ArrayList<>(); ?

Is there any down side of using any of these? Or writing List<String> list = new ArrayList<String>();

Pshemo
  • 122,468
  • 25
  • 185
  • 269
anonimos
  • 39
  • 6
  • 2
    The first should result in a compiler warning about using raw types, don't do that. Number 3 used to be the way to do it until the shorthand version (number 2) was introduced in java 8 (or java7?), so use either of these. – f1sh Jan 17 '23 at 09:35
  • 2
    `List list` is *raw type*. Please see [What is a raw type and why shouldn't we use it?](https://stackoverflow.com/q/2770321) – Pshemo Jan 17 '23 at 09:43

1 Answers1

0

Either:

List <String> list = new ArrayList<>();

Or:

List <String> list = new ArrayList<String>();

Both are correct.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
sourav
  • 1
  • 1