Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #34569

Re: Are you sure, Mr. Gosling?

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Are you sure, Mr. Gosling?
Date 2016-06-08 10:41 -0400
Organization A noiseless patient Spider
Message-ID <nj9arf$l3v$1@dont-email.me> (permalink)
References (9 earlier) <5c864ac4-9634-4c88-9d6b-d14dbf3b3886@googlegroups.com> <drodg0FejbU1@mid.individual.net> <746532a2-c6db-4d58-afc0-1cc57440eff5@googlegroups.com> <nj7asv$j9j$1@dont-email.me> <9ecc34ba-fac1-45ed-88cb-71403c7c906d@googlegroups.com>

Show all headers | View raw


On 6/8/2016 9:31 AM, Eric Douglas wrote:
> On Tuesday, June 7, 2016 at 4:30:34 PM UTC-4, Mike Amling wrote:
>> If you need obj1, obj2 and obj3 to themselves be cloned rather than just
>> copied, it gets more complicated.
>
> By cloned you mean copied by value and by copied you mean copied by reference?

     "Copied" is "whatever makes sense for the class."  For some classes
it makes sense for the "copy" to have the same field values as the
original; this is very likely the case if the fields are primitives or
are references to immutable objects.  If some of the original's fields
refer to mutable objects you may need to have the copy refer to copies
of those mutable objects -- but not necessarily; it all depends on what
your class "means."

> A copy constructor has the same amount of runtime/memory overhead as a clone method?

     "Probably not, except when it does.  That is to say, Yes.  Or No."

     In other words, either a constructor or a clone() method can execute
as many or as few statements as it likes, churn through as much or as
little memory as it likes, do as much I/O as it likes, query as many
web servers in Elbonia as it likes, ...

> 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.

     Cleanliness is in the eye of the beholder.  (So is grit.)

> A copy constructor by default creates an instance of the current class.  A clone method by default creates an instance of Object, 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.

     More precisely, a constructor "creates" nothing at all.  The `new'
keyword creates an object of a specified class, and then the chain of
constructors initialize that object.  (That's why Object's constructor,
for example, sometimes initializes a String and sometimes a JButton.)

     As for the "default" behavior of clone() -- Well, the "default"
behavior is to create and throw a CloneNotSupportedException.  If you
implement Cloneable and write a clone() method, that method has some
kind of behavior -- but it's hard to call it a "default" behavior.

     You may choose to specify that your clone() implementation returns
a Gizmo instead of an Object (if that's your preference), and assuming
the returned object is obtained from super.clone() then yes: At some
point along the line somebody must apply a cast.  However, I'd suggest
returning an Object instead, because that puts the fewest constraints
on the clone() methods of subclasses -- they might want to return a
non-Gizmo, and if your class' clone() returns Gizmo they'll be unable
to override it.  If your class is final, yeah, maybe then you could
have clone() return a Gizmo -- but if not, I'd strongly recommend
sticking with Object.  (It would be rather strange for a clone() to
return something of a different class than the cloned "leaf" object,
but clone() is itself rather strange, IMHO.  Stay on the safe side.)

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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