The meaning of a = b in object oriented languages

Lew lew at lewscanon.com
Wed Sep 19 17:52:59 EDT 2007


Lew wrote:
>> Java is a strongly-typed, compiled language which means it does more
>> static type checking and thus would reject treating a as both an array
>> and a String.
>>   In that environment the programmer must choose one or the other.

Ken Bloom wrote:
> In this Java example, a and b are statically typed to be of type Object. 
> Both Strings and Arrays [sic] descend from Object. (And primatives [sic] like 
> integers and the like will be autoboxed into descendants of Object).

That doesn't make Strings and arrays assignment compatible, and besides, it 
isn't so.

It was not stated by the OP that they were statically typing the variable to 
Object.  In fact, their example used a type "Animal".  They also used the 
syntax "a[1]", which is not possible in Java with a variable of type Object. 
So clearly a and b are /not/ statically typed to Object.

Unless you meant "/if/ a and b are statically typed [to] Object" that the 
assignment will work, which is true but of sharply limited usefulness.

In that case, you are a) defeating Java's type system and b) not getting the 
benefit of the Stringness or arrayness of the variable.  This is in contrast 
to the dynamically-typed languages wherein the variable will behave like an 
object of the runtime type, unlike in Java.

Back to the OP's example:
Summercool wrote:
>> so that's why  a[1] = "foobar"  will change what b will display, but 
>> a = "foobar" will not change what b will display.

Again, this cannot be done in Java.  The same variable a cannot be set to a 
String and still be used with array syntax.

-- 
Lew



More information about the Python-list mailing list