Below are the keypoints i learned in this chapter.
1) In section 10.3 Array Creation it states
It is a compile-time error if the element type of the Array is not a reifiable type (§4.7)
Below program won't compile as expected because ArrayList
import java.util.ArrayList;
import java.util.List;
public class ArrayDemo {
public static void main(String[] args) {
ArrayList<String> objArr[]=new ArrayList<String>[10];
List<String> listString=new ArrayList<String>();
listString.add("Demo");
List<?> listOne=listString;
objArr[0]=listOne;
objArr[1]=listOne;
for (Object object : listOne) {
ArrayList<String> each=(ArrayList<String>)object;
for (String string : each) {
System.out.println(string);
}
}
}
}
However,if we change component type to a reifiable type it works!!!.
import java.util.ArrayList;
import java.util.List;
public class ArrayDemo {
public static void main(String[] args) {
List<?> objArr[]=new ArrayList<?>[10];
List<String> listString=new ArrayList<String>();
listString.add("Demo");
List<?> listOne=listString;
objArr[0]=listOne;
objArr[1]=listOne;
for (Object object : listOne) {
ArrayList<String> each=(ArrayList<String>)object;
for (String string : each) {
System.out.println(string);
}
}
}
}
2) In section 10.6 Array Initializers it states
A trailing comma may appear after the last expression in an array initializer and is ignored.
class Test {
public static void main(String[] args) {
int ia[][] = { {1, 2}, null };
for (int[] ea : ia)
for (int e: ea)
System.out.println(e);
}
}
prints:
1
2
before causing a NullPointerException in trying to index the second component of the array ia, which is a null reference
3) In 10.7 Array Members section it states
A clone of a multidimensional array is shallow, which is to say that it creates only a single new array. Subarrays are shared.
class Test {
public static void main(String[] args) throws Throwable {
int ia[][] = { { 1 , 2}, null };
int ja[][] = ia.clone();
System.out.print((ia == ja) + " ");
System.out.println(ia[0] == ja[0] && ia[1] == ja[1]);
}
}
which prints:
false true
4) In section 10.10 Array Store Exception it states
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.
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
Here the variable pa has type Point[] and the variable cpa has as its value a reference to an object of type ColoredPoint[]. A ColoredPoint can be assigned to a Point; therefore, the value of cpa can be assigned to pa.
A reference to this array pa, for example, 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.
Hope this explanation helps if any.
2 comments:
1) The example you provide for "1) Section 14.3" does not compile for me, so the JLS is correct. What was the point you were trying to illustrate?
2) You apparently do not know what a qualified name is: A qualified name is an identifier prefixed by package names and class names, separated by dots, such as java.awt.Color.RED. RED is a field, and java.awt.Color.RED is a fully-qualified name. Color.RED would be a qualified, although not fully qualified name.
In none of your examples do you use a qualified name for x, so your examples have nothing to do with "A local variable cannot be referred to using a qualified name".
I stopped reading here, because I felt like I was wasting my time. Perhaps you should stop wasting your time WRITING and spend it on READING some more.
Mathias,
Thanks for your suggestions.
1) For the example i was trying to state that the first version of the program won't compile as it is not a refiable type hence need to implement the immediate 2nd version.
However,i will try to illustrate correctly and more elaboratly next time i write a entry.
Post a Comment