Array Element Assignment
Case 1: In case of primitive types as array elements we can provide any type which can be implicitly promoted to declared type.
ex: int[] x = new int[4];
x[0] = 12;
x[1] = 'a';
byte b = 13;
x[2] = b;
short s = 34;
x[3] = s;
x[4] = 16L; // CE: Possible loss of precision
found long required int
found long required int
In case of float array the allowed data type are byte, short, char, int long float.
Case 2: In case of Object type arrays, As array elements we can provide either declare type objects or its child class objects.
Ex: Object[] obj = new Object[10];
obj[0] = new Object();
obj[1] = new Integer(1);
obj[2] = new String("Arrays");
Ex: Number[] num = new Number[10];
num[0] = new Integer(10);
num[1] = new Double(10.2);
num[2] = new String("Arrays"); CE: Incompatible Types foud java.lang.String required java.lang.Number
Case 3: For interface type arrays as array elements , its implementation class objects are allowed.
Runnabale[] r = new Runnable[2];
r[0] = new Thread();
r[1] = new String("Arrays"); CE: Incompatible Types foud java.lang.String required java.lang.Runnable
for ex char element can be promoted to int type whereas char array cannot be promoted to int array.
eg: char[] ch = {'a','b','c'};
int[] x = ch; CE: incompatible types found char[] required int[].
Questions: Which of the following promotions will be performed automatically.
char to int //valid
char[] to int //invalid
int to double // valid
int[] to double[] //invalid
float to int //invalid
float[] to int[] // invalid
String to Object // valid
String[] to Object[] // valid
But in the case of Object type arrays child class type array can be promoted to Parent class type array.
String[] s = {"a","aa"};
Object[] c = s;
Case 2: Whenever we are assigning one array to another array internal elements won't be copied just reference variable will be reassigned.
ex: int[] a = {1,2,3,4};
int[] b = {1,2};
a = b; valid
b = a; valid
Case 3: Whenever we are assigning one array to another array dimensions must be matched.
for ex: In the place of int[] we should provide One dimensional array only, If we are trying to provide any other dimension then we will get compile time error.
int[][] x = new int[3][2];
x[0] = new int[4][3]; CE: incompatible types found int[][] required int[]
x[0] = 10; incompatible type found int required int[]
x[0] = new int[10]; valid
Wheneever we are assigning one array to another array both dimensions of the type must be matched but sizes are not required to match.
QUESTION
class Test{
public static void main(String [] srgs){
for(int i =0; i <= args.length;i++){
System.out.println(args[i]);
}
}
}
Java Test A B C :
A
B
C
RE: IndexOutOfBoundsException
Java Test :
RE: IndexOutOfBoundsException
class Test{
public static void main(String [] srgs){
String[] argh = {"x","Y","Z"};
args = argh;
for(String s in args){
System.out.println(s);
}
}
}
Java Test A B C :
X
Y
Z
Java Test :
X
Y
Z
int[][] x = new int[4][3];
a[0] = new int[4];
a[1] = new int[2];
a = new int[3][2];
Ques: How many objects are created ? --> 11
Ques: How many objects eligible for Garbage Collection ? --> 7
ARRAY VARIABLE ASSIGNMENT
Case 1: Element level promotions are not applicable at array levelfor ex char element can be promoted to int type whereas char array cannot be promoted to int array.
eg: char[] ch = {'a','b','c'};
int[] x = ch; CE: incompatible types found char[] required int[].
Questions: Which of the following promotions will be performed automatically.
char to int //valid
char[] to int //invalid
int to double // valid
int[] to double[] //invalid
float to int //invalid
float[] to int[] // invalid
String to Object // valid
String[] to Object[] // valid
But in the case of Object type arrays child class type array can be promoted to Parent class type array.
String[] s = {"a","aa"};
Object[] c = s;
Case 2: Whenever we are assigning one array to another array internal elements won't be copied just reference variable will be reassigned.
ex: int[] a = {1,2,3,4};
int[] b = {1,2};
a = b; valid
b = a; valid
Case 3: Whenever we are assigning one array to another array dimensions must be matched.
for ex: In the place of int[] we should provide One dimensional array only, If we are trying to provide any other dimension then we will get compile time error.
int[][] x = new int[3][2];
x[0] = new int[4][3]; CE: incompatible types found int[][] required int[]
x[0] = 10; incompatible type found int required int[]
x[0] = new int[10]; valid
Wheneever we are assigning one array to another array both dimensions of the type must be matched but sizes are not required to match.
QUESTION
class Test{
public static void main(String [] srgs){
for(int i =0; i <= args.length;i++){
System.out.println(args[i]);
}
}
}
Java Test A B C :
A
B
C
RE: IndexOutOfBoundsException
Java Test :
RE: IndexOutOfBoundsException
class Test{
public static void main(String [] srgs){
String[] argh = {"x","Y","Z"};
args = argh;
for(String s in args){
System.out.println(s);
}
}
}
Java Test A B C :
X
Y
Z
Java Test :
X
Y
Z
int[][] x = new int[4][3];
a[0] = new int[4];
a[1] = new int[2];
a = new int[3][2];
Ques: How many objects are created ? --> 11
Ques: How many objects eligible for Garbage Collection ? --> 7
Comments
Post a Comment