copy constructor question

X

xarax

mark said:
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.
 
C

Chris Smith

mark said:
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
 
S

Stefan Ram

Chris Smith said:
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++.
 
L

Liz

Stefan Ram said:
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, "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.
 
C

Chris Smith

Stefan said:
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, "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
 
S

Stefan Ram

Chris Smith said:
Stefan Ram wrote: (...)
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".
 
G

Gary Labowitz

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top