String array initialization in Java [duplicate]
Asked Answered
G

5

82

If I declare a String array:

String names[] = new String[3];

Then why can't we assign values to the array declared above like this:

names = {"Ankit","Bohra","Xyz"};
Grenada answered 7/7, 2013 at 18:39 Comment(1)
"array constants can be used only in initializers"; isn't that reason enough? Unless you're looking for "legislative intent" kind of stuff!Radcliff
C
150

You can do the following during declaration:

String names[] = {"Ankit","Bohra","Xyz"};

And if you want to do this somewhere after declaration:

String names[];
names = new String[] {"Ankit","Bohra","Xyz"};
Catty answered 7/7, 2013 at 18:40 Comment(4)
As I asked why we can't initialize it using curly braces after declaring as array of 3.Grenada
@AnkitBohra. That's what the 2nd snippet is doing.Catty
@AnkitBohra because {"x", "y", "z"} isn't an array instance. While new String[] {"x", "y", "z"} is.Radcliff
BTW, String names[] is equivalent to String[] names...Libriform
G
13
names[] = {"Ankit","Bohra","Xyz"};

is an initializer and used solely when constructing or creating a new array object. It cannot be used to set the array. You can use it when declared as:

String[] names= {"Ankit","Bohra","Xyz"};

You may also use:

names=new String[] {"Ankit","Bohra","Xyz"};
Glick answered 7/7, 2013 at 18:40 Comment(2)
how does it make difference when we have to use - String[] names= {"Ankit","Bohra","Xyz"}; and when names=new String[] {"Ankit","Bohra","Xyz"};, any use cases where does it fit into. thanks in advance @hexafraction.Sarette
@Avenger Nothing. They both do the same thing, if I remember correctly.Glick
R
6

First up, this has got nothing to do with String, it is about arrays.. and that too specifically about declarative initialization of arrays.

As discussed by everyone in almost every answer here, you can, while declaring a variable, use:

String names[] = {"x","y","z"};

However, post declaration, if you want to assign an instance of an Array:

names = new String[] {"a","b","c"};

AFAIK, the declaration syntax is just a syntactic sugar and it is not applicable anymore when assigning values to variables because when values are assigned you need to create an instance properly.

However, if you ask us why it is so? Well... good luck getting an answer to that. Unless someone from the Java committee answers that or there is explicit documentation citing the said syntactic sugar.

Radcliff answered 7/7, 2013 at 18:56 Comment(0)
G
3

You mean like:

String names[] = {"Ankit","Bohra","Xyz"};

But you can only do this in the same statement when you declare it

Granlund answered 7/7, 2013 at 18:41 Comment(2)
Not like that, I wanted to know why we can't do this curly braces initialization after declaring any array.Grenada
Because the language is defined in that way unfortunately.Granlund
F
2

It is just not a valid Java syntax. You can do

names = new String[] {"Ankit","Bohra","Xyz"};
Fritzfritze answered 7/7, 2013 at 18:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.