- what is the difference between:
String a[]andList<String> a? - Is
String a[]correct? or for defining array of strings we should useString[] aalways? - When we declare
String[] a, do we need to initialize it always?
Suppose i do this:
String[] a = new String[5];
a[2] = "Hello";
a[3] = "World";
a[2] = "Good Bye";
Since array is stored contiguously in memory and when i modify a string new string object is created. How does all these strings stored and modified in Java for all of the above statements?
String a[]is a native Java array ofStrings.List ais a raw (untyped)List(see The Java Collections). You probably want the genericised version:List<String> a. There are far too many differences to list here; you should follow that link!String a[]andString[] aare equivalent.String[] ais the convention, though.String[] ais actually a reference. If it’s a non-local variable, it will be implicitly initialised tonullif you don’t explicitly initialise it. You commonly will want to do something likeString[] a = new String[10];.String; the underlying data itself is not stored in the array.