[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.java.programmer

Re: Are you sure, Mr. Gosling?

Re: Are you sure, Mr. Gosling?

Robert Klemme

6/14/2016 5:11:00 PM

On 10.06.2016 21:20, Lew wrote:
> On Wednesday, June 8, 2016 at 11:55:13 AM UTC-7, Robert Klemme wrote:
>> On 08.06.2016 15:31, Eric Douglas wrote:

>>> Defaults from clone() to consider (if we don't want these, we either
>>> need to specify the alternative in the copy constructor, or replace in
>>> the clone method on the new instance returned from super.clone()):
>>> Immutable objects get copied by value, bucket objects get copied by
>>> reference.
>>
>> This is wrong. Objects ("reference types") are copied by reference by
>> Object.clone() - regardless of whether they are mutable or not. Only
>> primitive types are copied by value.
>
> That isn't correct. References are copied, by default, but they are
> copied by value. When 'clone' is overridden appropriately, those
> references are dereferenced, and the values to which they point are
> copied. However you look at it, it is values that are copied - either
> pointer values or primitive values.

Yes, references (pointers) are copied by value, i.e. there is a new
object that has a pointer to the same object. That is the default
behavior of clone(). This is different from what Eric had written (see
above) and which prompted my response.

Now, of course, if you override it typically you would retain some
references (e.g. to immutable objects) and provide new values for others
(e.g. to collections), typically by creating a new object one way or
another (i.e. invoking clone(), a constructor, a factory method or
something else) or using a default value (e.g. null).

> Copy-by-reference means that the original reference will point to the
> new object, which is not what we want nor what 'clone' does.

I am not sure. Now that we have talked about it so much I am not sure
any more that "copy by reference" is actually properly defined. It
seems a parallel construction to "call by reference" but that describes
another situation, namely a non-copy of a value. You could describe
this as copying an immutable pointer (in C++ you cannot change what a
reference points to, as opposed to a pointer).

> This is similar to the logic that proves that method arguments in
> Java are always passed by value. Java doesn't actually have
> references, as C++ programmers and others understand them. It has
> pointers.

Well, underneath all are pointers (to memory addresses). In C++ the
difference is that you can change a pointer (i.e. where it points to)
but not a reference. In that sense references in Java are really pointers.

Kind regards

robert


--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestprac...

Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Re: Are you sure, Mr. Gosling?

Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te

Il servizio è completamente GRATUITO!