M
mark
what is the equivalent in java of "copy constructor" that is used in C++ ?
what is the equivalent in java of "copy constructor" that is used in C++ ?
mark said:what is the equivalent in java of "copy constructor" that is used in C++ ?
mark said:what is the equivalent in java of "copy constructor" that is used in C++ ?
Chris Smith said: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
Your rigor is refreshing.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++.
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.
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++.
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;
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.
But it seems as if the creators of Java do not wish an
array itself (or an object) to be called "a variable".
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.