Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #34594
| Newsgroups | comp.lang.java.programmer |
|---|---|
| Date | 2016-06-10 12:20 -0700 |
| References | (10 earlier) <drodg0FejbU1@mid.individual.net> <746532a2-c6db-4d58-afc0-1cc57440eff5@googlegroups.com> <nj7asv$j9j$1@dont-email.me> <9ecc34ba-fac1-45ed-88cb-71403c7c906d@googlegroups.com> <drr804FhojnU1@mid.individual.net> |
| Message-ID | <60f13013-4df5-453d-bf62-78283e57e79b@googlegroups.com> (permalink) |
| Subject | Re: Are you sure, Mr. Gosling? |
| From | Lew <lewbloch@gmail.com> |
On Wednesday, June 8, 2016 at 11:55:13 AM UTC-7, Robert Klemme wrote:
> On 08.06.2016 15:31, Eric Douglas wrote:
> > On Tuesday, June 7, 2016 at 4:30:34 PM UTC-4, Mike Amling wrote:
>
> > If both methods return the same object, my understanding is the copy
> > constructor just looks cleaner if you have a lot of variables you want
> > as default values and the clone method will copy those by default so
> > should look cleaner if you have a lot of variables you want copied.
>
> That does not make sense: if you want to copy state then you do not want
> the default values. You want the actual values from the object.
> Actually, cloning and copying should preserve observable state.
>
> And if you have some state that is caching something and you want the
> cache (say a Map) to be empty in the copy then you need to assign the
> member with a freshly created instance in both cases. So there is no
> difference in amount of assignments needed.
>
> It may even be the other way round: clone() might require less
> assignments if there is state in immutable objects whose references can
> be shared.
>
> > A copy constructor by default creates an instance of the current
> > class. A clone method by default creates an instance of Object,
>
> NO! Please read what is written in this thread or the documentation of
> clone().
>
> > so
> > either the clone method of the Cloneable class must cast super.clone()
> > to it's own class, or everything that calls it's clone method must cast
> > the result.
>
> Yes and yes. Note that the API was defined before generics, that's why
> it is the way it is.
>
> > 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.
Copy-by-reference means that the original reference will point to the new object, which is not what we want nor what 'clone' does.
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.
--
Lew
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: Are you sure, Mr. Gosling? Arne Vajhøj <arne@vajhoej.dk> - 2016-06-04 15:27 -0400
Re: Are you sure, Mr. Gosling? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-04 16:43 -0400
Re: Are you sure, Mr. Gosling? Arne Vajhøj <arne@vajhoej.dk> - 2016-06-04 18:44 -0400
Re: Are you sure, Mr. Gosling? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-05 08:05 -0400
Re: Are you sure, Mr. Gosling? Arne Vajhøj <arne@vajhoej.dk> - 2016-06-05 08:31 -0400
Re: Are you sure, Mr. Gosling? Eric Douglas <e.d.programmer@gmail.com> - 2016-06-06 05:22 -0700
Re: Are you sure, Mr. Gosling? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-06 09:00 -0400
Re: Are you sure, Mr. Gosling? v_borchert@despammed.com (Volker Borchert) - 2016-06-06 16:06 +0000
Re: Are you sure, Mr. Gosling? Robert Klemme <shortcutter@googlemail.com> - 2016-06-06 22:56 +0200
Re: Are you sure, Mr. Gosling? Eric Douglas <e.d.programmer@gmail.com> - 2016-06-07 05:08 -0700
Re: Are you sure, Mr. Gosling? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-07 08:41 -0400
Re: Are you sure, Mr. Gosling? Martin Gregorie <martin@address-in-sig.invalid> - 2016-06-07 13:14 +0000
Re: Are you sure, Mr. Gosling? Eric Douglas <e.d.programmer@gmail.com> - 2016-06-07 06:30 -0700
Re: Are you sure, Mr. Gosling? Robert Klemme <shortcutter@googlemail.com> - 2016-06-07 19:10 +0200
Re: Are you sure, Mr. Gosling? Eric Douglas <e.d.programmer@gmail.com> - 2016-06-07 10:22 -0700
Re: Are you sure, Mr. Gosling? Mike Amling <mamling@chaff.us> - 2016-06-07 15:31 -0500
Re: Are you sure, Mr. Gosling? Eric Douglas <e.d.programmer@gmail.com> - 2016-06-08 06:31 -0700
Re: Are you sure, Mr. Gosling? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-08 10:41 -0400
Re: Are you sure, Mr. Gosling? Eric Douglas <e.d.programmer@gmail.com> - 2016-06-08 10:21 -0700
Re: Are you sure, Mr. Gosling? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-08 14:10 -0400
Re: Are you sure, Mr. Gosling? Robert Klemme <shortcutter@googlemail.com> - 2016-06-08 20:54 +0200
Re: Are you sure, Mr. Gosling? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-08 16:36 -0400
Re: Are you sure, Mr. Gosling? Robert Klemme <shortcutter@googlemail.com> - 2016-06-08 23:11 +0200
Re: Are you sure, Mr. Gosling? Lew <lewbloch@gmail.com> - 2016-06-10 12:20 -0700
Re: Are you sure, Mr. Gosling? Robert Klemme <shortcutter@googlemail.com> - 2016-06-14 19:10 +0200
Re: Are you sure, Mr. Gosling? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-06-07 17:12 -0400
Re: Are you sure, Mr. Gosling? Robert Klemme <shortcutter@googlemail.com> - 2016-06-07 23:25 +0200
Re: Are you sure, Mr. Gosling? Arne Vajhøj <arne@vajhoej.dk> - 2016-06-07 17:33 -0400
Re: Are you sure, Mr. Gosling? Arne Vajhøj <arne@vajhoej.dk> - 2016-06-06 21:18 -0400
csiph-web