Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How do I declare and initialize an array in Java?

How would I announce and instate an array in Java?

2 Answers

There are two ways of declaring an array in java:
type variable-name[];
type[] variable-name;

There are two ways of initializing an array in java:
1. Without assigning values
datatype [ ] arrayName = new datatype [size];

2. By assigning values

datatype [ ] arrayName={value1, value2, value3};
Type[] variableName = new Type[capacity];

Type[] variableName = {comma-delimited values};

Type variableName[] = new Type[capacity];

Type variableName[] = {comma-delimited values};
is also valid, but I prefer the brackets after the type, because it's easier to see that the variable's type is actually an array.

Login / Signup to Answer the Question.