groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jochen Theodorou <blackd...@gmx.org>
Subject Re: Casting Double to BigDecimal
Date Tue, 12 Jan 2016 08:39:39 GMT


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

Mime
View raw message