groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alberto Vilches <>
Subject Re: Casting Double to BigDecimal
Date Tue, 12 Jan 2016 10:01:11 GMT
Thank you! Now I know the difference between these two operators :) I
always though the Java-like cast do an "as type" behind the scenes. I think
is kind of confusing to have theoretically equivalent operator with
differents results. Deeping in the code I have another differents like []
as Set creates a LinkedHashSet and (Set)[] creates a HashSet. Anyway, I
don't mind the current behavior, but I really think it should be documented
(before to write the question in the mail list, I tried to find in the docs
with no results). Thank you again for your time, it's really appreciated.

On Tue, Jan 12, 2016 at 9:39 AM, 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

Un saludo.
Alberto Vilches

View raw message