Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java equivalent of C++ copy assignment operator

I'm trying to understand this operator function written in C++ and convert it to Java.

Class& Class::operator=(const Class& In) {

   properties = In.properties;

   return *this;

}

Does this simply copy instance and properties of a class object? For which I've already written something:

public static Class copy(Class obj) {
    //returns new instance of Class individual
    Class copy =  new Class(obj.row_num, obj.col_num, obj.input_length, obj.output_length, obj.max_arity, obj.function_length, obj.levels_back);
    copy.genes = obj.genes.clone();
    return copy;
}

Am I on the correct track? Many thanks for your help.

like image 222
p.drewello Avatar asked Oct 17 '13 13:10

p.drewello


People also ask

What is the assignment operator in Java?

Assignment operators are used in Java to assign values to variables. For example, int age; age = 5; Here, = is the assignment operator. It assigns the value on its right to the variable on its left.

Does Java copy on assignment?

In Java, there is no operator to create a copy of an object. Unlike C++, in Java, if we use the assignment operator then it will create a copy of the reference variable and not the object.

Is == an assignment operator?

What is the difference between = (Assignment) and == (Equal to) operators. The “=” is an assignment operator is used to assign the value on the right to the variable on the left. The '==' operator checks whether the two given operands are equal or not. If so, it returns true.


3 Answers

Ampersand & designates a reference in C++. It is needed to provide the behavior similar to what Java objects provide "out of the box", because Java manages objects through references.

There is no copying going on in C++ when a reference is passed. In fact, avoiding copying is a major reason for using const references as function parameters.

The code that you show does not perform copying either: it changes its state based on the value being "assigned". The closest way of modeling this in Java would be providing an assign(Class other) method that changes the current state to match that of the object passed in:

Class assign(Class other) {
    this.properties = other.properties;
    return this;
}

You will need to use this method in place of C++'s assignment, like this:

Class clOne(args1);
Class clTwo(args2);
clOne = clTwo;      // Using the assignment operator

becomes this:

Class clOne = new Class(args1);
Class clTwo = new Class(args2);
clOne.assign(clTwo); // Using the assignment method instead of the operator
like image 60
Sergey Kalinichenko Avatar answered Oct 10 '22 01:10

Sergey Kalinichenko


You're pretty much on the right track. The copy assignment operator in C++ is used when directly assigning (copying) from one object to another. As Java objects are only accessible via references, such assignments are meaningless. To match the C++ semantics exactly, the Java equivalent would be:

public Class copy(Class obj) {
    row_num = obj.row_num;
    col_num = obj.col_num;
    // etc., etc.
    genes = obj.genes.clone();
    return this;
}
like image 37
Angew is no longer proud of SO Avatar answered Oct 10 '22 00:10

Angew is no longer proud of SO


Am I on the correct track?

Kind of. But not quite. C++ distinguishes between reassigning an existing object and creating a new one.

Java doesn’t. You cannot reassign to an existing object in Java1 (but you can of course reassign a reference). In Java, in order to copy an object (rather than assign a reference to it), you would usually use a copying constructor:

Class(Class other) {
    // Copy members of `other` into `this`.
}

And then use it as follows:

Class x = new Class(something here);
Class y = new Class(x); // copy

In particular, this is what all the Java containers implement. I would not rely on clone. First of all, clone should only be used if the class implements the tag interface Cloneable. Second of all, clone’s design is arguably broken and its use is not recommended.


1 Well you could of course reassign the members of an object (unless they are final), and you could mimic C++’s copy assignment operator by providing a method assign to do that. However, this isn’t the conventional way of doing things in Java (although it might have its place in some exceptional instances).

like image 2
Konrad Rudolph Avatar answered Oct 09 '22 23:10

Konrad Rudolph