How to initialize a static array?

I have seen various approaches to defining a static array in Java. Or:

String[] suit = new String[] { "spades", "hearts", "diamonds", "clubs" }; 

... or only

 String[] suit = { "spades", "hearts", "diamonds", "clubs" }; 

or as List

 List suit = Arrays.asList( "spades", "hearts", "diamonds", "clubs" ); 

Is there any difference (other than defining a list, of course)?

What is the best way (performance)?

+54
java arrays static playing-cards
Aug 08 2018-11-11T00:
source share
2 answers

If you create an array, then there is no difference, however the following:

 String[] suit = { "spades", "hearts", "diamonds", "clubs" }; 

But if you want to pass an array to a method, you should call it like this:

 myMethod(new String[] {"spades", "hearts"}); myMethod({"spades", "hearts"}); //won't compile! 
+90
Aug 08 2018-11-11T00:
source share
— -

No, no difference. It is just syntactic sugar . Arrays.asList(..) creates an additional list.

+8
Aug 08 2018-11-11T00:
source share



All Articles