All About Arrays
Declaring an arrayAn array variable is like other variables -- you must declare it, which means you must declare the type of elements that are in an array. All elements must be the same type. Write the element type name, then "[]", then the name of the array variable. The declaration only allocates space associated with a variable name for a reference to an array, but doesn't create the actual array object.
String[] args; // args is an array of Strings int[] scores; // scores is an array of ints JButton[] bs; // bs is an array of Jbuttons
Unlike some languages, never put the size of the array in the declaration because an array declaration only tells Java that the variable is an array and the element type. Allocating an array object Create an array using new. This example creates an array of 100 int elements, from a[0] to a[99].
int[] a; // Declares a to be an array of ints a = new int[100]; // Allocates an array of 100 ints
Length of an array
Each array has a constant (final) instance variable that has its length. You can find out how many elements an array can hold by writing the array name followed by .length. In the previous example, a.length would be 100. Remember that this is the number of elements in the array, one more than the maximum subscript.
Initial array element values -- zero/null/false
When an array is allocated (with new), all elements are set to an initial value. The initial value is 0 if the element type is numeric (int, float, ...), false for boolean, and null for all object types.
Array variables are references to arrays
When you declare an array variable, Java reserves only enuf memory for a reference (Java's name for an address or pointer) to an array object. When an array object is created with new, a reference is returned, and that reference can then be assigned to a variable. Similarly, you can assign one array variable to another, and it is only the reference that is copied. For example,
int[] a = new int[]{100,99.98};
int[] b;
// "a" points to an array, and "b" doesn't point to anything
b = a; // now b points t the same array as "a"
b[1] = 0; // also changes a[1] because a and b are the same array.
// Both a and b point to same array with value {100,0,98}
Array Initialization
When you declare an array, you can also allocate a pre-initialized array object in the same statement. In this case, do not give the array size because Java counts the number of values to determine the size.
String[] days = {"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"};
FAQ Home
Sponsored links
Build IT Knowledge with Current & Trusted Content
Helps Employees Develop & Hone New Technical Programming Skills. Sign Up & Get Full Access.
Helps Employees Develop & Hone New Technical Programming Skills. Sign Up & Get Full Access.
Check Out IT Certification Preparation Materials
Sign Up With SkillSoft & Get Access to Training Materials for Over 50 Professional Certifications.
Sign Up With SkillSoft & Get Access to Training Materials for Over 50 Professional Certifications.
SFTP components for .NET
Add complete SSH and SFTP support to your .NET framework application
Add complete SSH and SFTP support to your .NET framework application
Virtual File System SDK
Create your own file systems in Windows and .NET applications
Create your own file systems in Windows and .NET applications
PureCM Software Configuration Management
Version control and integrated issue tracking - powerful and easy to use. Get your FREE trial now!
Version control and integrated issue tracking - powerful and easy to use. Get your FREE trial now!
