Rohit Bajaj
asked on
Difference between runtime Type and compile time type of an object
HI,
Following is an example from effective java joshua bloch :
And he mentions :
The compile time type could be String[], Integer[] that is understandable as user will pass a String[] or integer[] etc...
But what does it mean the runtime type is Object[] and why is that..
wont user will pass a String[] for example to the method param.. Then during runTime also the type will be String[] ?
what exactly does it mean ?
Following is an example from effective java joshua bloch :
// Naive generic version of reduction - won't compile!
static <E> E reduce(List<E> list, Function<E> f, E initVal) {
E[] snapshot = list.toArray(); // Locks list
E result = initVal;
for (E e : snapshot)
result = f.apply(result, e);
return result;
}
And he mentions :
The compile- time type of snapshot is E[] which could be String[], Integer[], or any other kind of array. The runtime type is Object[], and that’s dangerous.
The compile time type could be String[], Integer[] that is understandable as user will pass a String[] or integer[] etc...
But what does it mean the runtime type is Object[] and why is that..
wont user will pass a String[] for example to the method param.. Then during runTime also the type will be String[] ?
what exactly does it mean ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Asked and answered.