Static Array in JavaIn Java, array is the most important data structure that contains elements of the same type. It stores elements in contiguous memory allocation. There are two types of array i.e. static array and dynamic array. In this section, we will focus only on static array in Java. Static ArrayAn array that is declared with the static keyword is known as static array. It allocates memory at compile-time whose size is fixed. We cannot alter the static array. If we want an array to be sized based on input from the user, then we cannot use static arrays. In such a case, dynamic arrays allow us to specify the size of an array at run-time. Static Array ExampleFor example, int arr[10] creates an array of size 10. It means we can insert only 10 elements; we cannot add a 11th element as the size of Array is fixed. Advantages of Static Array
Disadvantages of Static Array
Declaring a Static ArrayThe syntax to declare a static array is: For example: We can also declare and initialize static array as follows: Static array can also be declared as a List. For example: Static Array Java ProgramStaticArrayExample.java Output: Welcome to Javatpoint Let's see another Java program. StaticArrayExample.java Output: 1 2 3 4 5 Difference Between Static Array and Dynamic ArrayThe following table describes the key differences between static array and dynamic array.
Next TopicWater Jug Problem in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India