Consider the foll eg:-
I] class print{
public static void print(long arr[]){
for (int i =0; i< arr.length; i++)
System.out.println(arr[i]);
}
public static void print(long arr){
System.out.println(arr);
}
public static void main(
String args[]){
int i[] = {1,2,2,3,4};
print(i); //line 1
int t = 3;
print(t);
}
}
Why does line marked line 1 not compile..If I want to pass an int array , how do I pass it to the function print??..
II] class a
{
};
class b extends a
{
};
class print{
public static void print(b arr[]){
for (int i =0; i< arr.length; i++)
System.out.println(arr[i]);
}
public static void main(String args[]){
//b arr[] = {new b(), new b()};
// print(arr);
Object arr[] = {new b(), new b()};
print((b[])arr);
}
}
If the variable arr is of any other type other than b (say of type a(superclass) or of type Object), even on casting it to an array of objects of type b , it gives a ClassCastExceptionError? Can anyone please explain why and the solution to the same??
Can anyone tell em of some good eg�s or notes on Object referencing, conversion & casting.I read from rHE , but am confused.