copy constructor question

Discussion in 'Java' started by mark, Jun 5, 2004.

  1. mark

    mark Guest

    what is the equivalent in java of "copy constructor" that is used in C++ ?
    mark, Jun 5, 2004
    #1
    1. Advertising

  2. mark

    Roedy Green Guest

    On 5 Jun 2004 15:06:10 -0700, (mark) wrote or
    quoted :

    >what is the equivalent in java of "copy constructor" that is used in C++ ?


    Arrgh. Long winded, error-prone, field by field copies!!!


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jun 5, 2004
    #2
    1. Advertising

  3. mark

    xarax Guest

    "mark" <> wrote in message
    news:...
    > what is the equivalent in java of "copy constructor" that is used in C++ ?


    An instance of a class/struct/union does not exist in
    Java in the same sense as a C++ class/struct/union (in
    the context of your question). Only a reference to an
    instance. Therefore, your question is a non-sequitor
    in the Java context.

    However, there is the protected instance method clone()
    defined in the base class Object that can be overridden
    by a subclass to instantiate a new instance and assign
    the appropriate values to the appropriate fields.

    Read the JavaDoc on the Cloneable interface for more
    details.
    xarax, Jun 6, 2004
    #3
  4. mark

    Chris Smith Guest

    mark wrote:
    > what is the equivalent in java of "copy constructor" that is used in C++ ?
    >


    Mark,

    Xarax's answer is right, but a little terse. I'll expand a bit.

    In C++, a variable can be either an object or a pointer to the object.
    In Java, that's not true; a variable, if it has anything to do with an
    object at all, is a reference that points to that object. One of the
    several problems that copy constructors are meant to solve in C++ is
    what happens when you make a copy of a variable (e.g., via pass-by-value
    to a method) that is an object. That problem doesn't arise in Java,
    because variables can only be references, and it's always obvious how to
    copy a reference (and the result points to the same object as the
    original). So there's no copying of objects done "behind the scenes" in
    Java, as would be the case with C++.

    However, programmers in C++ also use copy constructors to explicitly
    make a copy of an object. This is a need that exists in Java as well,
    and there is a way to deal with it: the clone() method. Java provides
    clone() as a replacement for copy constructors because copy constructors
    can be very difficult to use when polymorphism is prevalent in the
    design; you don't need to know the exact concrete subclass of an object
    in order to clone() it, whereas you do in order to copy it correctly
    using a copy constructor.

    The problem with clone() is that it was declared protected in Object,
    meaning that you need to specifically allow cloning when you define a
    class by overriding it with a public version. You'd additionally need
    to meet the contract of the clone() method, of course, and the easiest
    way to do this is to implement the Clonable interface and start out by
    calling Object's clone() implementation before making any needed
    modifications on the result. A convenient side-effect of Object.clone()
    -- in addition to creating the right class of object for you -- is that
    it provides a shallow copy of fields, but any deep copying needs to be
    done explicitly.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Jun 6, 2004
    #4
  5. mark

    Stefan Ram Guest

    Chris Smith <> writes:
    >In C++, a variable can be either an object or a pointer to the object.


    C++ is defined by ISO/IEC 14882:1998(E).

    In C++, "a variable is introduced by the declaration of an
    object." -- ISO/IEC 14882:1998(E), 3p4

    "An object is a region of storage." -- ISO/IEC 14882:1998(E),
    1.8p1.

    >In Java, that's not true; a variable, if it has anything to do with an
    >object at all, is a reference that points to that object. One of the


    In Java, "A variable is a storage location". -- JLS 4.5

    "An object is a class instance or an array." -- JLS 4.3.1

    So, the meaning of "variable" in Java corresponds with the
    meaning of "object" in C++.

    In C++, a "variable" always is a name, as introduced by its
    declaration, while in Java a "variable" might be an anonymous
    region of storage, like a component of an array - this is
    called an "object" in C++. In C++ "int i;" declares an object,
    which is not an instance of any class, but still an object in
    the C++ sense.

    In C++, a declaration like "ExampleClass * p;" declares an
    object, but the object is not an instance of the class
    "ExampleClass", but a pointer storage for a pointer to
    such an instance. While this storage is not an "object" in
    the JLS-sense, it is an "object" in the C++ sense. In this
    sense a variable is always an object in C++.
    Stefan Ram, Jun 6, 2004
    #5
  6. mark

    Liz Guest

    "Stefan Ram" <-berlin.de> wrote in message
    news:-berlin.de...
    > Chris Smith <> writes:
    > >In C++, a variable can be either an object or a pointer to the object.

    >
    > C++ is defined by ISO/IEC 14882:1998(E).
    >
    > In C++, "a variable is introduced by the declaration of an
    > object." -- ISO/IEC 14882:1998(E), 3p4
    >
    > "An object is a region of storage." -- ISO/IEC 14882:1998(E),
    > 1.8p1.
    >
    > >In Java, that's not true; a variable, if it has anything to do with an
    > >object at all, is a reference that points to that object. One of the

    >
    > In Java, "A variable is a storage location". -- JLS 4.5
    >
    > "An object is a class instance or an array." -- JLS 4.3.1
    >
    > So, the meaning of "variable" in Java corresponds with the
    > meaning of "object" in C++.
    >
    > In C++, a "variable" always is a name, as introduced by its
    > declaration, while in Java a "variable" might be an anonymous
    > region of storage, like a component of an array - this is
    > called an "object" in C++. In C++ "int i;" declares an object,
    > which is not an instance of any class, but still an object in
    > the C++ sense.
    >
    > In C++, a declaration like "ExampleClass * p;" declares an
    > object, but the object is not an instance of the class
    > "ExampleClass", but a pointer storage for a pointer to
    > such an instance. While this storage is not an "object" in
    > the JLS-sense, it is an "object" in the C++ sense. In this
    > sense a variable is always an object in C++.
    >

    Your rigor is refreshing.
    Liz, Jun 6, 2004
    #6
  7. mark

    Chris Smith Guest

    Stefan Ram wrote:
    > Chris Smith <> writes:
    > >In C++, a variable can be either an object or a pointer to the object.

    >
    > C++ is defined by ISO/IEC 14882:1998(E).
    >
    > In C++, "a variable is introduced by the declaration of an
    > object." -- ISO/IEC 14882:1998(E), 3p4
    >
    > "An object is a region of storage." -- ISO/IEC 14882:1998(E),
    > 1.8p1.


    You're right up to this point. To clarify what I said earlier, by "an
    object" in my earlier post I was referring to an object of a specific
    user-defined class, in the context of the question. Technically, the
    pointer would *also* be its own object. (At least, that's true in the
    sense used by the C++ language specification here; OO theorists would
    question whether either of the "objects" we're talking about are objects
    at all, since they don't really own their identity.)

    > >In Java, that's not true; a variable, if it has anything to do with an
    > >object at all, is a reference that points to that object. One of the


    > In Java, "A variable is a storage location". -- JLS 4.5
    >
    > "An object is a class instance or an array." -- JLS 4.3.1
    >
    > So, the meaning of "variable" in Java corresponds with the
    > meaning of "object" in C++.


    No, that's not even remotely true. In C++, the concept of "object"
    _includes_ things whose closest analogue in Java is a variable, but it
    _also_ includes things whose closest analogue in Java is an object. The
    object/variable distinction doesn't go by different names in C++; it
    simply doesn't exist at all.

    The quote you gave from section 4.5 of the JLS is taken entirely out of
    context here. If you quoted the entire sentence ("A variable is a
    storage location and has an associated type, sometimes called its
    compile-time type, that is either a primitive type or a reference
    type.") it would be obvious that being "a storage location" is not the
    entire definition of a variable in Java; what makes something a variable
    is about its having a primitive or reference type associated with it,
    not merely being a piece of storage. That restricts the term to a very
    specific set of things; namely, local variables, instance and class
    fields, and array elements.

    In fact, a Java object is *not* a variable, which is directly related to
    the fact that many implicit uses of copy constructors in C++ simply
    don't translate to Java, exactly as I said before.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Jun 6, 2004
    #7
  8. mark

    Stefan Ram Guest

    Chris Smith <> writes:
    >Stefan Ram wrote: (...)
    >> In Java, "A variable is a storage location". -- JLS 4.5

    >The quote you gave from section 4.5 of the JLS is taken entirely out of
    >context here. If you quoted the entire sentence ("A variable is a
    >storage location and has an associated type, sometimes called its
    >compile-time type, that is either a primitive type or a reference
    >type.") it would be obvious that being "a storage location" is not the
    >entire definition of a variable in Java;


    If a sentence of the form "A ^ B" (where "^" is the logical
    and-operation) is true, it follows that the assertion "A" is
    true.

    So, when "something is a storage location /and/ has an
    associated type" is true, then "that something is a storage
    location" is also true.

    In Java, a variable /is/ a storage location, notwithstanding
    the fact that it also has an associated type.

    On the other hand, you are right insofar as not every storage
    location (e.g., a part of another variable) is a variable,
    unless it also has a type.

    >what makes something a variable is about its having a primitive
    >or reference type associated with it, not merely being a piece
    >of storage. That restricts the term to a very specific set of
    >things; namely, local variables, instance and class fields, and
    >array elements.


    Yes, but what else could it be? I mean, are there any other
    Java entities, which are storage locations but have no type?
    Only parts of variables (e.g., the most significant half an
    int variable) come to my mind, but these do not appear as
    entities in the Java language.

    BTW: According to the JLS the variables contained within an
    array are called its "components" - not its "elements". See
    paragraph 2 of chapter 10.

    >In fact, a Java object is *not* a variable, which is directly related to
    >the fact that many implicit uses of copy constructors in C++ simply
    >don't translate to Java, exactly as I said before.


    An object, in Java, surely has a type. So to determine whether
    it is a variable, one needs to figure out whether it is a
    storage location. While an object might be used to store data,
    it might not be considered as a (single) /location/ of storage
    (fields might be stored distributed in non consecutive memory
    regions). Therefore, one might be able to conclude that an
    object is /not/ a variable, indeed.

    I am not sure, whether a whole array is a variable, it has a
    type. It also is a storage location, possibly a multi-byte
    storage location, but then other variables are also extended
    across multiple bytes. (Where a "byte" is supposed to be the
    smallest addressable unit of the JVM.)

    However, the JLS explicitly mentions "Array Variables" in
    10.2, meaning variables holding a /reference/ to an array,
    which does not exclude that an array might also be a variable.
    The JLS only explicitly asserts that array components and
    "array variables" are variables, it does not assert nor
    deny explicitly that the whole array is a variable.
    But it seems as if the creators of Java do not wish an
    array itself (or an object) to be called "a variable".
    Stefan Ram, Jun 6, 2004
    #8
  9. "Stefan Ram" <-berlin.de> wrote in message
    news:eek:-berlin.de...
    > Chris Smith <> writes:
    > >Stefan Ram wrote: (...)

    <<giant snip>>
    > But it seems as if the creators of Java do not wish an
    > array itself (or an object) to be called "a variable".
    >


    Well, here I come. (Fools rush in...)
    Indeed, an object is not a variable. A variable is the concept of an area of
    storage that holds a value. The size of that area and the encoding used for
    the bits in that area and determined by the type.
    An object is an area of storage that contains some data (consisting of
    variables, or optionally none), some methods (named code blocks, or none),
    and any and all control structures required for the object-referencing
    system to find, evaluate access, and modify or transfer control to those
    data and methods.
    Thus: variables have type; objects have class.
    These are all Java terms.

    I won't try to mess with C++ terminology, since it is a) off topic, and b)
    off topic.
    --
    Gary
    Gary Labowitz, Jun 6, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VisionSet
    Replies:
    8
    Views:
    4,874
    Tris Orendorff
    Apr 29, 2004
  2. Aire
    Replies:
    3
    Views:
    454
    Mike Wahler
    Jan 25, 2004
  3. ali
    Replies:
    4
    Views:
    558
    David Harmon
    Mar 5, 2007
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,197
  5. cinsk
    Replies:
    35
    Views:
    2,559
    James Kanze
    Oct 11, 2010
Loading...

Share This Page