• Post Reply Bookmark Topic Watch Topic
  • New Topic

Different ways to initialize an array  RSS feed

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've seen multiple ways to initialize arrays in Java:



I understand that the first way is useful when you don't know the values you want in the array when you initialize it, and the second way is good when you do. However, I don't understand the difference between the second and the third way. Why should one be preferred over the other, and when should I use one rather than the other?
 
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

The only thing I can think off is to specify the type of the array....



output:
false
true
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The difference between the first and second methods in OP's post are that one creates an array with elements and the other an empty array. You can see that so much better with arrays of reference types than of primitives. If you have code like this (from this post):-If you print that out with methods of the Arrays class you get the obvious output:-What you have here is a declaration and array initialiser later. The normal form of an array initialiser is this:-
words = new String[]{"Most", "ranchers", "want", "to", "strangle", "Campbell"};
but if you have the declaration and initialisation on the same line you are allowed to miss part of it out:-
String[] words = {"Most", "ranchers", "want", "to", "strangle", "Campbell"};
So that would have the same effect on the words array, and the second and third versions of Miles Davis' code will have the same effect.

The first line however declares and initialises the same array but does not initalise the individual elements. In the case of an array of primitives, the default values might be usable; the int[] shown will be filled with zeroes. But in the case of an array of primitives, the default values are nulls (see the thread I got the earlier code from). It is potentially dangerous, and probably poor practice, to have nulls, but you can see what happens if you try a different initialisation of that words array.-I would say try to put known values into the array; if you don't know the values get them assigned as soon as you can. Avoid arrays full of nulls as far as possible. Avoid nulls as far as possible.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few minutes ago, I wrote:. . . In the case of an array of primitives, the default values might be usable . . .
If you print out an array of primitives, you can see the effect:-
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell,

Sorry I do not understand your reply, see my previous post to what I believe was a valid answer to the OP question which was:


However, I don't understand the difference between the second and the third way. Why should one be preferred over the other, and when should I use one rather than the other?


So here, again an example of the difference between the second and the third way:


Hello,

The only thing I can think off is to specify the type of the array....




output:
false
true
[1, 2.0]
[1, 2.0]

Cheers,


 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Miles Davis wrote:

I understand that the first way is useful when you don't know the values you want in the array when you initialize it, and the second way is good when you do.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am afraid your replies were not at all helpful because they do not answer the OP's question.
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I am afraid your replies were not at all helpful because they do not answer the OP's question.


He asked for the difference between 2 and 3 and you explained the difference between 1 and 2. Do you have a better answer for the difference between 2 and 3 and why you should use one over the other, other than the answer I already provided in the first reply?

"3 allows you to specify the type of the Array"

Again, here the arrays are sorted differently if you use 2 instead of 3:


output:
[111, 111111, 2, 22]
[2, 22, 111, 111111]

 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A.J. Côté wrote:. . . "3 allows you to specify the type of the Array" . . .
Not in the example which OP gave; he gave an array of primitives where you cannot specify the type like that.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the example you gave works and shows your point, but I don't think it is what OP was asking.
 
Miles Davis
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:The difference between the first and second methods in OP's post are that one creates an array with elements and the other an empty array. You can see that so much better with arrays of reference types than of primitives. If you have code like this (from this post):-If you print that out with methods of the Arrays class you get the obvious output:-What you have here is a declaration and array initialiser later. The normal form of an array initialiser is this:-
words = new String[]{"Most", "ranchers", "want", "to", "strangle", "Campbell"};
but if you have the declaration and initialisation on the same line you are allowed to miss part of it out:-
String[] words = {"Most", "ranchers", "want", "to", "strangle", "Campbell"};
So that would have the same effect on the words array, and the second and third versions of Miles Davis' code will have the same effect.

The first line however declares and initialises the same array but does not initalise the individual elements. In the case of an array of primitives, the default values might be usable; the int[] shown will be filled with zeroes. But in the case of an array of primitives, the default values are nulls (see the thread I got the earlier code from). It is potentially dangerous, and probably poor practice, to have nulls, but you can see what happens if you try a different initialisation of that words array.-I would say try to put known values into the array; if you don't know the values get them assigned as soon as you can. Avoid arrays full of nulls as far as possible. Avoid nulls as far as possible.


So essentially, the second and third ways of initializing an array are different in that the second is used only when the declaration is on the same line, and the third is used when the declaration is on a different line? If this is true, then should I just always use the third way since that is the more general case of the two?
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They both result in the same array in the case you posted (as opposed to the case that A.J. Côté posted).

It really doesn't matter which is used.
 
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot get subtypes in those examples because you cannot declare an array type which is a subtype of int[].
You can create arrays which are subtypes of their declared type. But you cannot get subtypes of String[] because the String class is final. I shall have to go back to AJC's examples:-Note the number literals are converted to Objects by boxing and remember arrays have unoverridden toString methods so you can see their runtime type.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!