Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Static and Dynamic Array Initialization

Is it true that every array that is initialized during runtime is dynamic and every array that is initialized during compiling is static?

for example:

int array[];                 
public main() {      
    array = new int[100];    
}

the compiler knows how many elements the array has so it can initilize it during compiling right? or do i need to give every int a value so it becomes static? like this:

int array[3] { 1, 2, 3};              

and is it posible to define how many elements an array should have outside the main() function? (without giving every int a value) like this:

int array[100];      
public main() {
}

I am programming a little game and it has to run really fast. I read dynamic arrays need a bit longer to process so i want to try it with static arrays, but I am not sure when an array becomes static or dynamic. I searched in many diffrent tutorials but i couldn't find an answer for that.
thanks for reading.

like image 604
Jebeto Avatar asked Apr 18 '13 06:04

Jebeto


People also ask

What is static and dynamic array in Java?

Overview. A fixed array is an array for which the size or length is determined when the array is created and/or allocated. A dynamic array is a random access, variable-size list data structure that allows elements to be added or removed.

What is difference between static array and dynamic array?

Difference Between Static Array and Dynamic ArrayStatic arrays are allocated memory at compile time. Dynamic array is located at run-time. The size of static array is fixed. The size of dynamic array is fixed.


2 Answers

The distinction of dynamic and static allocation is ambiguous (it somewhat depends on the language what it means). In the most general sense, static allocation means that some size has been predetermined, maybe at compile time.

In java, any objects (that includes arrays) are always allocated at runtime. That doesn't necessarily mean it's dynamic, it may still be static in the sense that it can't be changed at runtime. Example:

public class Test1 {
    public final int[] array1 = new int[10];

    public int[] array2 = new int[20];

    public void setArray2Size(int size) {
         array2 = new int[size];
    }
}

The array1 is of size 10, and that can't be changed at runtime. Do note that the final keyword. This lets you assign the "array1" member only once. So you can't assign a different array to this member.

Now, array2 is not final, so you can at any point assign a different array to it, like the setArray2Size()-method does. If there were no assignment after the initial assignment, array2 would still be static in the sense that it can't be changed (because there is no code to do so), although by declaration changing it is allowed.

A concrete instance of an array can not be resized ever once created (there is no language element to express resizing an array in java). It is somewhat hard to grasp for beginners, but a variable like array2 is not the array. It's a reference to the array. You can however replace the reference that array2 holds with the reference to another array, as shown for array2 in setArray2Size()-method.

like image 130
Durandal Avatar answered Oct 13 '22 20:10

Durandal


Memory is not allocated at the time of declaration.

eg: int array[100]; ; -- won't compiles

its only when new operator is encountered memory is allocated.

So

array = new int[100]; ; compiles.

like image 39
Suresh Atta Avatar answered Oct 13 '22 21:10

Suresh Atta