How to declare and initialize an array in Java?

Posted by Jennifer | Updated on

Want to declare and initialize an array in Java? You can either use array declaration or array literal (but only when you declare and affect the variable right away, array literals cannot be used for re-assigning an array).

For classes, for example String, it’s the same:

The third way of initializing is useful when you declare the array first and then initialize it. The cast is necessary here.


If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.

Related Questions & Answers