The meaning of a = b in object oriented languages

Lew lew at
Wed Sep 19 00:02:59 CEST 2007

Summercool wrote:
> when a writing or a book reads "a is a Hash object; a is an Array
> object; or a is an Animal object" it is just a short form to say that
> "a is a reference to that object."
> b = a means "whatever a is referencing to, now b is referencing it
> too".
> so that's why  a[1] = "foobar"  will change what b will display, but
> a = "foobar" will not change what b will display.  

You can't do both in Java.  Is a an array or a String?  If a is a String and b 
is an array, then neither `a = b' nor `b = a' will compile in Java.

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.

Otherwise what you say is exactly correct.

> (because a[1] = "foobar" says "what is  a  referencing?  go there and change its
> content that has the index 1"  and when b goes there to see it, it is
> also changed.)

Speaking just of Java, it's useful to distinguish a variable from an object 
(instance).  As you point out, the variable represents a reference to the 
instance.  The variable has a compile-time type in Java, which may be 
different from the run-time type of the object, albeit compatible.

C++ is similar in this respect.  Python and Ruby are more, shall we say, 
flexible in their type systems.

Both jet liners and hang gliders have their uses, both are flight, and neither 
is really suitable for the other's purpose.


More information about the Python-list mailing list