Link to home
Start Free TrialLog in
Avatar of Rohit Bajaj
Rohit BajajFlag for India

asked on

Difference between runtime Type and compile time type of an object

HI,
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;
}

Open in new window


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
Avatar of dpearson
dpearson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dpearson
dpearson

Asked and answered.