Declarations and Access Control (1)
Objective 1
Write code that declares, constructs and initializes arrays of any base type using any of the permitted forms, both for declaration and for initialization.
1. Arrays are Java objects. (An object is a class instance or an array.) and may be assigned to variables of type Object. All methods of class Object may be invoked on an array. If you create an array of 5 Strings, there will be 6 objects created.
2. Arrays should be
1. Declared. (int[] a; String b[]; Object []c; Size should not be specified now)
2. Allocated (constructed). ( a = new int[10]; c = new String[arraysize] )
3. Initialized. for (int i = 0; i < a.length; a[i++] = 0)
3. The above three can be done in one step. int a[] = { 1, 2, 3 }; or int a[] = new int[] { 1, 2, 3 }; But never specify the size with the new statement.
4. Java arrays are static arrays. Size has to be specified at compile time. Array.length returns array’s size, cannot be changed. (Use Vectors for dynamic purposes).
5. Array size is never specified with the reference variable, it is always maintained with the array object. It is maintained in array.length, which is a final instance variable. Note that arrays have a length field (or property) not a length() method. When you start to use Strings you will use the string, length method, as in s.length();
6. Anonymous arrays can be created and used like this: new int[] {1,2,3} or new int[10]
7. Arrays with zero elements can be created. args array to the main method will be a zero element array if no command parameters are specified. In this case args.length is 0.
8. Comma after the last initializer in array declaration is ignored.
int[] i = new int[2] { 5, 10}; // Wrong
int i[5] = { 1, 2, 3, 4, 5}; // Wrong
int[] i[] = {{}, new int[] {} }; // Correct
int i[][] = { {1,2}, new int[2] }; // Correct
int i[] = { 1, 2, 3, 4, } ; // Correct
int i[][] = new int [10][]; //Correct, i.length=10.
int [] i, j[] == int i[], j[][];
9. Array indexes start with 0. Index is an int data type.
10. Square brackets can come after datatype or before/after variable name. White spaces are fine. Compiler just ignores them.
11. Arrays declared even as member variables also need to be allocated memory explicitly.
static int a[];
static int b[] = {1,2,3};
public static void main(String s[]) {
System.out.println(a[0]); // Throws a null pointer exception
System.out.println(b[0]); // This code runs fine
System.out.println(a); // Prints ‘null’
System.out.println(b); // Prints a string which is returned by toString
}
12. Once declared and allocated (even for local arrays inside methods), array elements are automatically initialized to the default values.(0 for numeric arrays, false for boolean, '\0' for character arrays, and null for objects).
13. If only declared (not constructed), member array variables default to null, but local array variables will not default to null(compile error).
14. Java doesn’t support multidimensional arrays formally, but it supports arrays of arrays. From the specification - “The number of bracket pairs indicates the depth of array nesting.” So this can perform as a multidimensional array. (no limit to levels of array nesting)
15. Arrays must be indexed by int values; short, byte, or char values may also be used as index values because they are subjected to unary numeric promotion and become int values. An attempt to access an array component with a long index value results in a compile-time error.
16. All array accesses are checked at run time; an attempt to use an index that is less than zero or greater than or equal to the length of the array causes an ArrayIndexOutOfBoundsException to be thrown.
17. Every array implements the interfaces Cloneable and java.io.Serializable.
18. Array Store Exception
If an array variable v has type A[], where A is a reference type, then v can hold a reference to an instance of any array type B[], provided B can be assigned to A.
Thus, the example:
class Point { int x, y; }
class ColoredPoint extends Point { int color; }
class Test {
public static void main(String[] args) {
ColoredPoint[] cpa = new ColoredPoint[10];
Point[] pa = cpa;
System.out.println(pa[1] == null);
try {
pa[0] = new Point();
} catch (ArrayStoreException e) {
System.out.println(e);
}
}
}
produces the output:
true
java.lang.ArrayStoreException
Testing whether pa[1] is null, will not result in a run-time type error. This is because the element of the array of type ColoredPoint[] is a ColoredPoint, and every ColoredPoint can stand in for a Point, since Point is the superclass of ColoredPoint.
On the other hand, an assignment to the array pa can result in a run-time error. At compile time, an assignment to an element of pa is checked to make sure that the value assigned is a Point. But since pa holds a reference to an array of ColoredPoint, the assignment is valid only if the type of the value assigned at run-time is, more specifically, a ColoredPoint. if not, an ArrayStoreException is thrown.
19. Every element of an array must be of the same type The type of the elements of an array is decided when the array is declared. If you need a way of storing a group of elements of different types, you can use the collection classes.
Tags:
作者:佚名评论内容只代表网友观点,与本站立场无关!
评论摘要(共 0 条,得分 0 分,平均 0 分)
查看完整评论