Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between Java and C++ copy constructor

I am curious how Java supports copy constructor and how is it different from C++? I would like to understand the Java equivalent of the logic to execute rule of three (copy constructor, destructor, assignment operator) from the compiler side of the story.

like image 921
ND_27 Avatar asked Sep 28 '13 02:09

ND_27


People also ask

What is the difference of normal constructor and copy constructor in Java?

Constructor: It is a method which has the same name as the class which is used to create an instance of the class. Copy Constructor: Used to create an object by copying variables from another object of the same class. The main purpose is to create a new object from an existing one by copying variables.

Is there a copy constructor in Java?

In Java, a copy constructor is a special type of constructor that creates an object using another object of the same Java class. It returns a duplicate copy of an existing object of the class. We can assign a value to the final field but the same cannot be done while using the clone() method.

What is the difference between copy constructor and clone in Java?

Copy Constructor vs. However, the copy constructor has some advantages over the clone method: The copy constructor is much easier to implement. We do not need to implement the Cloneable interface and handle CloneNotSupportedException. The clone method returns a general Object reference.

What is difference between copy constructor and copy assignor?

The difference between a copy constructor and an assignment operator is that a copy constructor helps to create a copy of an already existing object without altering the original value of the created object, whereas an assignment operator helps to assign a new value to a data member or an object in the program.


2 Answers

Java has no specific linguistic support for copy constructors. Rather you just code the state copying by hand in the constructor; e.g.

public class Person {
    private String firstName;
    private String lastName;

    public Person(Person other) {
        this.firstName = other.firstName;
        this.lastName = other.lastName;
    }
    ...
}

I would like to understand the Java equivalent of the logic to execute rule of three (copy constructor, destructor, assignment operator) from the compiler side of the story.

The copy constructor is as above. Is really just a (simple) design pattern.

Java doesn't provide an equivalent to C++ assignment operator loading. Java supports assignment of primitive types and reference types, but not assignment of objects in the same way that C++ does. It is unusual for special actions are required when assigning a value in Java. And in the cases where you need to do this, it is customary to put the logic into a setter method.

Java supports finalize methods, that are similar in some respects to C++ destructors. The primary differences are that finalize methods are operations on objects not reference variables, and they are typically called a long time after the object's last reference has gone out of scope.

However, you rarely need to use finalize methods:

  • Java is a fully garbage collected language, and the best strategy for memory management is to just let the GC take care of it.

  • Other resources are best managed using "try / finally" or "try with resources".

AFAIK, the only sound use-case for finalize methods is for cleaning up resources that have been lost accidentally; e.g. because someone forgot to "close" them using the recommended mechanisms.

like image 112
Stephen C Avatar answered Sep 30 '22 17:09

Stephen C


Java only has references to objects. You can't declare inline (on stack or in object) objects.

copy constructor

Java doesn't have one. You can write one but this is rarely done.

, destructor

Java doesn't have one. It has finalize() but this is highly discouraged.

assignment operator

Java doesn't have one for objects, only references to objects.

like image 42
Peter Lawrey Avatar answered Sep 30 '22 17:09

Peter Lawrey