What is the difference between Copy Constructor and Assignment Operator?

The main difference between copy constructor and assignment operator is that Copy constructor is a type of constructor that helps to create a copy of an already existing object without affecting the values ​​of the original object whereas assignment operator is an operator that helps to assign a new value to a variable in the program.

A constructor is a special method that helps initialize an object when creating it. It has the same name as the class name and has no return type. A programmer can write a constructor to give initial values ​​to instance variables in the class. If there is no constructor in the program, the default constructor will be called. Copy constructor is a type of constructor that helps create a copy of an existing object. On the other hand, the assignment operator helps to assign a new value to a variable.

Key Areas Covered

1. What is Copy Constructor?
     – Definition, Functionality
2. What is Assignment Operator?
     – Definition, Functionality
3. What is the difference between Copy Constructor and Assignment Operator?
     – Comparison of key differences

Key terms

Constructor, Copy Constructor, Assignment Operator, Variable

What is Copy Constructor

In programming, it is sometimes necessary to create a separate copy of an object without affecting the original object. Copy constructor is useful in these situations. Allows you to create a replica of an existing object of the same class. See the example below.

Figure 1: Program with constructor copy

The Triangle class has two instance variables called base and height. On line 8, there is a parameterized constructor. Two arguments are needed. These values ​​are assigned to the base and height instance variables. On line 13, there is a copy constructor. A triangle type argument is required. The new base value of the object is assigned to the base of the instance variable. Similarly, the new height value of the object is assigned to the height of the instance variable. Also, there is a method called calArea to calculate and return the area.

In the main method, t1 and t2 are Triangle objects. The t1 object is passed when creating the t2 object. The copy constructor is called to create the t2 object. Therefore, the base and height of object t2 are the same as the base and height of object t1. Finally, both objects have the same area.

What is the assignment operator?

An assignment operator is useful for assigning a new value to a variable. The assignment operator is “=”. When there is a statement like c = a + b; the sum of ‘a’ and ‘b’ is assigned to the variable ‘c’.

Figure 2: Program with assignment operator

The class number has an instance variable called num. There is no parameter constructor on line 7. However, there is a parameterized constructor on line 9. It takes an argument and assigns it to the instance variable using the assignment operator. On line 12, there is a method called display to display the number. In the main method, num1 and num2 are two objects of type Number. Printing num1 and num2 provides the references to those objects. Number 3 is of type Number. On line 24, num1 is assigned to num3 using the assignment operator. Therefore, num3 refers to object num1. Therefore, the printout num3 gives the reference num1.

The assignment operator and its variations are as follows.

Operator functionality Example
= Assigns the right operand to the left operand  z = x + y
+= Add the right operand to the left operand and assign the result to the left operand z + = y is equivalent to z = z + y
– = Subtract the right operand from the left operand and assign the result to the left operand. z – = y is equivalent to

z = z -y

* = Multiplies the right operand with the left operand and assigns the result to the left operand. z * = y is equivalent to

z = z * y

/ = Divide the left operand with the right operand and assign the answer to the left operand. z / = y is equivalent to

z = z / y

% = Take the two-operand modulus and assign the answer to the left operand. z% = y is equivalent to

z = z% y

<<= Left shift and assignment operator z << 5 is equivalent to

z = z <<5

>> = Shift right and operator z >> 5 is equivalent to

z = z >> 5

& = Bit assignment operator and AND z & = 5 is equivalent to

z = z & 5

^ = Assignment operator and bitwise exclusive OR z ^ = 5 is equivalent to

z = z ^ 5

| = Assignment operator and bitwise inclusive OR z | = 5 is equivalent to z = z | 5

Difference between Copy Constructor and Assignment Operator

Definition

Copy constructor is a special constructor to create a new object as a copy of an existing object. In contrast, the assignment operator is an operator that is used to assign a new value to a variable. These definitions explain the basic difference between the copy constructor and the assignment operator.

Object functionality

Object functionality is also an important difference between the copy constructor and the assignment operator. The copy constructor initializes the new object with an existing object, while the assignment operator assigns the value of an object to another object that already exists.

Use

The copy constructor helps to create a copy of an existing object, while the assignment operator helps to assign a new value to a variable. This is another difference between copy constructor and assignment operator.

conclusion

The difference between copy constructor and assignment operator is that copy constructor is a type of constructor that helps to create a copy of an already existing object without affecting the values ​​of the original object while assignment operator is a operator that helps to assign a new value to a variable in the program.

Reference:

1. Thakur, Dinesh. “Copying the constructor in the Java example.” Computer notes, available here.

See More:

Leave a Reply

Your email address will not be published.

CAPTCHA


Back to top button