How to Initialize String Array in Java
An array of Strings is just like any other array but has String objects as its elements. As with other kind of arrays, the String array can be initialized is declared as:
String[] strArray;
Here we are declaring strArray as a reference variable. At this point, no memory has been allocated to any String object.
To initialize a String array, we need to use the new operator as shown below:
String[] strArray = new String[10];
We need to specify the size of array at compile time (while writing the program). We could also use constants in order to specify the size of the array being created. The constants can be created by using the final variable.
After using the above statement, 10 String objects are created and are initialized to null.
To provide values to each of the String object, use the index of the array as shown below:
strArray[0] = "Str1"; strArray[1] = "Str2"; ........................... strArray[9] = "Str10"
Other way to initialize string objects in a string array, the following piece of code can be used:
String[] strArray = new String {"str1",>"str2","str3",str4","str5","str6","str7","str8",>"str9","str10",};Here we are defining a String array object and providing values to individual String objects as well.
This is Extreme Java?
ReplyDelete1) Nothing is too small.
ReplyDelete2) According to my research, its helpful to all those who come searching for this topic but land on "What happened to equals .....
3) Curse Google
Agree with Sandeep , nothing is too small , from my favorite head first book , "No question is dumb question" and I see helping beginners is better than helping expert.
ReplyDeleteJavin
Why String is immutable in Java