H
H
Helcurt2020-12-25 22:45:42
Java
Helcurt, 2020-12-25 22:45:42

Question about arrays in Java, and understanding the difference between C++ and Java?

Integer[] arr = new Integer[5];

Integer arr1[];

Is there any difference between these arrays? I used to study C++ and I'm not used to the syntax of the first array, because this is how C++ allocated memory for a dynamic array. Does Java allocate dynamic memory for all arrays? C++ is a static language and Java is OOP, but I don't fully understand the difference between the two. OOP has classes, although C++ also has classes. In short, I am very confused. Help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2020-12-25
@Radia

Integer[] arr = new Integer[5];   // так вы объявляете массив длинной 5 элементов.
Integer[] arr1; // переменная arr1 объявлена, но не инициализирована.

You can write like this:
Integer[] arr;
arr = new Integer[5];

If you want dynamic arrays, look into collections. Java has collections for all occasions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question