Why doesn't Java have a copy constructor?

Why doesn't Java support copy constructor like in C ++?

+63
java c ++ copy-constructor
May 6 '09 at 2:34
source share
9 answers

Java does. They simply are not called implicitly, as in C ++, and I suspect your real question.

Firstly, the copy constructor is no more than:

public class Blah { private int foo; public Blah() { } // public no-args constructor public Blah(Blah b) { foo = b.foo; } // copy constructor } 

Now C ++ will indirectly call the copy constructor with an expression like this:

 Blah b2 = b1; 

Cloning / copying in this case simply does not make sense in Java, because all b1 and b2 are links, not objects like in C ++. In C ++, this statement creates a copy of the state of an object. In Java, it just copies the link . The state of the object is not copied, so implicitly calling the copy constructor does not make sense.

And that’s all really.

+128
May 6 '09 at 2:50
source share

From Bruce Eckel :

Why does [copy constructor] work in C ++ and not Java?

The copy constructor is a fundamental part of C ++, as it automatically makes a local copy of the object. Nevertheless, the example above proves that it does not work for Java. What for? In Java, all we manipulate is that, and in C ++, manual objects, and you can also bypass objects directly. What C ++ copy constructor is for: when you want to take an object and pass it by value, thus duplicating the object. This way it works fine in C ++, but you must ensure that this scheme does not work in Java, so do not use it.

(I recommend reading the entire page - in fact, instead of here )

+13
May 6 '09 at 2:48 a.m.
source share

I think the answer to this question is very interesting.

First, I believe that in Java all objects are on the heap, and as long as you don't have pointers, you have "Links". Links have copy symposia, and java internally tracks the number of links so the garbage collector knows what to get rid of.

Since you access only objects using copy links, the actual number of times you need to copy an object is significantly reduced (for example, in C ++, simply passing an object to a function (by value) leads to the creation of new objects, only a link is passed to Java per object). Designers suggested that clone () would be sufficient for other applications.

+8
May 6 '09 at 2:45
source share

This is just my opinion (I'm sure there is a reasonable answer)

C ++ copy constructors are primarily useful when you send or return class instances by value, since this happens when the copy constructor is transparently activated.

Since in Java everything is returned by reference, and the virtual machine is focused on dynamic allocation, there really was no justification for the complexity of the copy constructor.

In addition, since everything is by reference, the developer often has to provide his own implementation and decision on how to clone the fields.

+2
May 6 '09 at 2:46 a.m.
source share

Guess they decided that you can just make the clone () method?

+1
May 6 '09 at 2:39 a.m.
source share

It is somehow. When small copies are fine, you have [clone ()] ( http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html#clone ()) , and when they are not needed, you need to implement a deep copy just like C ++.

The only significant difference is that it is a factory method, not its own constructor, but in terms of flexibility and verifiability, which is probably good.

0
May 6 '09 at 2:46
source share

I am not a very C ++ programmer, but it seems that I remember the rule of "three amigos" - copy constructor, assignment operator and destructor. If you have one, you'll probably need all three.

So, perhaps, without a destructor in the language, they did not want to include a copy constructor? Just suppose.

0
May 6 '09 at 2:47 a.m.
source share

Well maybe. It simply is not created implicitly. If I had to guess, this is probably due to the fact that Java objects are always allocated as heaps.

In C ++, the default instance constructor is a small batch copy. If the class owns the memory allocated on the heap (using the raw pointer), this will force the copy to share the originals with the original, which is not what you want.

Imagine that Java has this behavior. Any class that has fields that are objects (read: essentially all of them) will have the wrong behavior, and you will need to override it yourself. In 99% of cases, you have not helped anyone. In addition, you just created a subtle trap for yourself - imagine that you accidentally forgot to override the default copy constructor. If it was created by default and you are trying to use it, the compiler will not complain at all, but your program will behave badly at runtime.

Even if they created a default copy constructor that performs a deep copy, I'm not sure if that would be particularly useful. Not only do you usually make fewer copies in Java than C ++, but you do not always want to copy a field.

The objects that you just own, and the objects that you refer to because you need them but are not responsible for them, are the same - just fields. Property and borrowing are not first class concepts. For the objects that you have, you want to copy them (if they are not immutable, in which case you should not worry), and for the objects to which you simply keep the link, you want to copy the link.

I would say that the copy constructor, which simply thoughtlessly deeply copies everything, is not suitable for many classes. Of course, more than shallow copying by default.

0
Aug 29 '16 at 18:26
source share

Java has a copy constructor
Note: instead of the demo d2 = new demo (d1) you can write a demo d2 = d1
The main difference is b / w two
demo d2 = new demo (d1) means that a new object is being created, and this is allocated memory. But
demo d2 = d1 means only creating a reference variable that uses the same memory address of d1 and therefore d2 is not assigned to shared memory.

Copy constructor syntax:
See below. Example First copying the constructor is very simple :))
classname (int datafield) // Simple constructor
{
this.datafield = DataField;
}

classname (class object)
{
datafield = object.datafield; // See below is an example
}
Now for calls
<b> {

classname obj = new classname ();

classname anotherObject = obj; // or classname anotherObject = new classname (obj)

}

  class demo
 {
     private int length;

     private int breadth;

     private int radius;

     demo (int x, int y)

     {
         length = x;
         breadth = y;
     }
     int area ()
     {
         return length * breadth;
     }

     // Copy Constructor
     demo (demo obj)
     {
         length = obj.length;
         breadth = obj.breadth;
     }


     public static void main (String args [])
     {
         demo d1 = new demo (5,6);
         demo d2 = new demo (d1); // Invokes Copy Constructure
         System.out.println ("Area for d1 object =" + d1.area ());
         System.out.println ("Area for d2 object =" + d2.area ());

     }
 }

-one
Jul 07 '13 at 22:31
source share



All Articles