java - Array of primitives or objects initialization -



java - Array of primitives or objects initialization -

array of string objects can created

// acceptable declarations , initialization

line 1: string[]s = new string[2]; line 2: string[]s = new string[]{"a","b"};

// below init looks me compiler errors out

line 3: string[] s = new string[2] { "a", "b" };

1) why cant specify size of array in line 3? 2) when create array using line 3, strings "a" , "b" created on heap or in string pool?

you can't both initialize array , specify size, redundant.

all string literals stored in constant pool , happens before code runs, @ class loading time.

also note new string[] redundant initializer:

string[] s = {"a","b"};

java string initialization

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -