Avatar ·

How to declare and initialize an array in Java?

Hello everyone! I just started learning Java and encountered confusion about declaring arrays. In different examples, I see different syntaxes and can't understand which one is correct and when to use what. Here's what I tried: ```java int numbers[]; int[] numbers; int[] numbers = new int[5]; int numbers = {1, 2, 3}; ``` The first option compiles, but some books say it's wrong to do it that way. The second option also works, but I don't understand the difference. The third option creates an array, but I don't know how to fill it with values except by writing `numbers[0] = 1` and so on for each element. The fourth option gives a compilation error. Please explain how to properly declare and initialize arrays in Java? In which cases should which syntax be used?