Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > copy constructor question

Reply
Thread Tools

copy constructor question

 
 
mark
Guest
Posts: n/a
 
      06-05-2004
what is the equivalent in java of "copy constructor" that is used in C++ ?
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      06-05-2004
On 5 Jun 2004 15:06:10 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (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.
 
Reply With Quote
 
 
 
 
xarax
Guest
Posts: n/a
 
      06-06-2004
"mark" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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.


 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      06-06-2004
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
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      06-06-2004
Chris Smith <(E-Mail Removed)> 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++.

 
Reply With Quote
 
Liz
Guest
Posts: n/a
 
      06-06-2004

"Stefan Ram" <(E-Mail Removed)-berlin.de> wrote in message
news:(E-Mail Removed)-berlin.de...
> Chris Smith <(E-Mail Removed)> 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.


 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      06-06-2004
Stefan Ram wrote:
> Chris Smith <(E-Mail Removed)> 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
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      06-06-2004
Chris Smith <(E-Mail Removed)> 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".

 
Reply With Quote
 
Gary Labowitz
Guest
Posts: n/a
 
      06-06-2004
"Stefan Ram" <(E-Mail Removed)-berlin.de> wrote in message
news(E-Mail Removed)-berlin.de...
> Chris Smith <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
template copy constructor vs normal copy constructor cinsk C++ 35 10-10-2010 11:14 PM
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
Calling base class constructor from derived class Copy constructor ali C++ 4 03-05-2007 09:15 AM
deep/shallow copy - constructor v Object.copy() VisionSet Java 8 04-29-2004 10:41 PM
Copy constructor hides default constructor Aire C++ 3 01-25-2004 05:47 PM



Advertisments