dictionary/hash and '1' versus 1

Diez B. Roggisch deets at nospam.web.de
Mon Jan 7 08:17:43 EST 2008


bearophileHUGS at lycos.com schrieb:
> Paddy:
>> Not really, it seems to me to be going the exact opposite way with
>> languages with automatic type conversions being seen as not suited for
>> larger programs.
> 
> In Java you can add the number 1 to a string, and have it
> automatically converted to string before the string join... What do
> you think of that feature?


This isn't really what is happening. In fact, in Java the +-operator is 
overloaded for strings to invoke the mandatory toString()-method on all 
objects when concatenated with a string.

So

"" + 1

works internally as

StringBuilder sb = new StringBuilder();
sb.append("");
sb.append(new Integer(1).toString());


Or something like that, depending on how optimizing the compiler is.

So you can also do

"" + some_object

However,

some_object + ""

or

1 + ""

don't work - the operator is only overloaded on the left argument.

Diez



More information about the Python-list mailing list