Jochen,
Should one thus prefer the usage of '(TYPE) x' over 'x as TYPE' in code?
On 12/01/2016 08:39, Jochen Theodorou wrote:
Am 12.01.2016 um 01:45 schrieb Schalk Cronjé:
I did not know there was even a difference. I tend to prefer 'as TYPE'
for IMO it reads better than '(TYPE)'. Maybe it is also left-over from
my C++ days where the latetr style cast was inherited from C and frowned
upon.
Is this 'cheap & fast' casting only affecting native types or is it
casting in general?
That's in general
an example to show how big the difference can be, even if they both
give the same result:
int i = 1
long l = (long) i
for a case like this we usually use the bytecode instrcution I2L
int i = 1
long l = i as long
This will call Integer.asType(long), most likely falling back to a
cast done by DefaultTypeTransformation. This means a dynamic method
call to asType, plus a long winded logic for the conversion itself
(there are worse cases involving reflection).
I2L is by orders cheaper as a dynamic method call.
bye Jochen
--
Schalk W. Cronjé
Twitter / Ello / Toeter : @ysb33r