A question on modification of a list via a function invocation

MRAB python at mrabarnett.plus.com
Tue Sep 5 15:56:46 EDT 2017


On 2017-09-05 20:08, Steve D'Aprano wrote:
> On Tue, 5 Sep 2017 11:47 pm, Gregory Ewing wrote:
> 
>> Steve D'Aprano wrote:
>>>     [quoting Scott Stanchfield]
>>>     Figure 7: (Java) Defining a Dog pointer
>>>     Dog d;
>>> 
>>>     When you write that definition, you are defining a pointer to a Dog
>>>     object, not a Dog object itself.
>>>     [end quote]
>>> 
>>> Here Scott mixes up what the compiler does (creates a pointer to a Dog
>>> object, and what the programmer's Java code does (creates a Dog).
>> 
>> Um, no. The declaration 'Dog d' on its own does NOT create a Dog,
>> in any way, shape or form. It only declares something that can
>> *refer* to a Dog created elsewhere, which is what Scott is
>> quite correctly saying.
> 
> That's not what he said. I quoted him: he quite clearly states that d defines a
> pointer to a Dog object. He doesn't say that you're declaring an empty slot
> that is waiting to be filled with a pointer to a dog. He says it defines a
> pointer.
> 
> So which Dog object does this pointer point to? Answer: there isn't one. There
> may not even be any Dog object existing in the entire program up to this point.
> 
> Even allowing that there were a Dog to be pointed to, how do we inspect or
> manipulate that pointer? Answer: you can't, because pointers are not meaningful
> values in Java.
> 
> There's no(?) Java code you can write that allows this:
> 
>      Dog d;
>      java.lang.System.out.println( address of variable d );
>      java.lang.System.out.println( address of the Dog d points to );
> 
> Maybe compiler-specific debugging tools? I don't know enough Java to
> *categorically* rule it out, but if it exists at all, it would be unsafe to
> rely on the addresses you get.
> 
d is initialised to null. You can check for null:

if (d == null)
     java.lang.System.out.println("There's no dog.");
else
     d.bark();



More information about the Python-list mailing list