Java - Static and Dynamic Array Initialization -


is true every array initialized during runtime dynamic , every array initialized during compiling static?

for example:

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

the compiler knows how many elements array has can initilize during compiling right? or need give every int value becomes static? this:

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

and posible define how many elements array should have outside main() function? (without giving every int value) this:

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

i programming little game , has run fast. read dynamic arrays need bit longer process want try static arrays, not sure when array becomes static or dynamic. searched in many diffrent tutorials couldn't find answer that.
reading.

the distinction of dynamic , static allocation ambigous (it depends on language means). in general sense, static allocation means size has been predetermined, maybe @ compile time.

in java, objects (that includes arrays) allocated @ runtime. doesnt mean dynamic, may still static in sense can't changed @ 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 arrayfixed of size 10, , can't changed @ runtime. note final keyword. lets assign "array1" member once. can't assign different array member.

now array2 not final, can @ point assign different array it, setarray2size()-method does. if there no assignment aftter initial assignment, array2 still static in sense cant changed (because there no code so), although declaration changing allowed.

a concrete instance of array can not resized ever once created (there no language element express resizing array in java). hard grasp beginners, variable array2 is not array. reference to array. can replace reference array2 holds reference array, shown array2 in setarray2size()-method.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -