Java. Operations on objects. The operation of assigning objects. Object comparison operations




Operations on objects. The operation of assigning objects. Object comparison operations


Contents


Search other websites:

1. What operations can be applied to objects in the Java programming language?

The Java programming language provides a wide range of operations that can be used for primitive data types and for objects.

For objects, you can apply the following operations:

  • assignment =;
  • comparison operation ==;
  • comparison operation !=.

2. How does the assignment of objects = is realized? General form

The assignment operation has the following general form:

obj1 = obj2;

here

  • obj1, obj2 – reference to objects that have the same type (class). When assigning objects, the value of the reference to the object obj2 is assigned to the reference obj1. In fact, only references are assigned, that is, the reference is copied from one place to another. After assignment, both references refer to the same memory location.

3. What is the difference between assignment (=) variables of primitive types and objects assignment?

The difference between the assignment of variables of primitive types and objects is as follows:

  • when assigning variables of primitive types, the value of one variable is copied to another. In this case, variables are placed in different parts of the memory. Changing the value of one variable will not change the value of another variable;
  • when assigning objects, only the reference to the object is copied and not the value of this object. That is, after performing the assignment of objects, references to these objects point to the same memory location. After assignment, changing object data by one reference will result in changing object data by another reference, since object reference refer to the same memory location.



4. What errors can occur when take place the assignment of references to objects?

If during the assignment the reference to the object in the left part pointed to some previously allocated memory section (by the operator new), then this section may be lost if there is no copy of this reference. After assignment, it will be impossible to obtain information about the data from this site. Later this site will be automatically cleared by the garbage collector.

5. Examples of using the assignment operator = for assigning class objects

Example 1. Let a class named SomeClass be given

// some class
class SomeClass {
    // internal data
    int d;
    float f;
    char c;

    // class constructor
    SomeClass(int d, float f, char c) {
        this.d = d;
        this.f = f;
        this.c = c;
    }
}

The following shows the assignment of objects of this class and the demonstration of access to the same memory:

// creating object obj1
SomeClass obj1 = new SomeClass(5, 3.8f, 'z');

// declaration of obj2 object of class SomeClass, for which no memory is allocated
SomeClass obj2 = null;

// assignment of references to objects
obj2 = obj1; // object references refer to the same memory location

int d = obj2.d; // d = 5
d = obj1.d; // d = 5

float f = obj2.f; // f = 3.8
f = obj1.f; // f = 3.8

char c = obj2.c; // c = 'z'
c = obj1.c; // c = 'z'
obj1.d = 280;
d = obj2.d; // d = 280
obj2.f = 7.777f;
f = obj1.f; // f = 7.777

Example 2. Sort an array of objects using the insertion sort. The example demonstrates the exchange between references to an object of type Data.

A Data class is specified that contains one internal ‘value’ variable and an explanation of the message to it of type String. Implement data sorting in descending order of value using the insertion sort.

The implementation of the Data class is as follows:

// the declaration of Data class
class Data {
    // class data
    int value;
    String message;

    // method that set a new values in class
    void Set(int val, String msg) {
        value = val;
        message = msg;
    }
}

Using an array of objects of type Data may be, for example, the following

// object array declaration
Data[] D = new Data[10]; // memory allocation for the entire Data array
Data tmp; // additional reference of type Data

// memory allocation for each object of array Data
for (int i=0; i<D.length; i++)
    D[i] = new Data();

// filling an array with arbitrary values
D[0].value = 5;
D[0].message = "value = 5";
D[1].value = 3;
D[1].message = "value = 3";
D[2].value = 7;
D[2].message = "value = 7";
D[3].value = 8;
D[3].message = "value=8";
D[4].value = 9;
D[4].message = "value => 9";
D[5].value = 10;
D[5].message = "value = 10";
D[6].value = 4;
D[6].message = "value = 4";
D[7].value = 6;
D[7].message = "value => 6 ";
D[8].value = 7;
D[8].message = "value = 7";
D[9].value = 2;
D[9].message = "value = two";

// array sorting in descending order
for (int i=0; i<D.length-1; i++)
    for (int j=i; j>=0; j--)
        if (D[j].value<D[j+1].value) {
            // swap - only object references are exchanged
            tmp = D[j]; // extra reference tmp is used
            D[j] = D[j+1];
            D[j+1] = tmp;
        }

// array output
for (int i=0; i<D.length; i++)
    System.out.println("D["+i+"]: value = " + D[i].value + "; message " + D[i].message);

As can be seen from the above example, between the objects D[j] and D[j+1] there is an exchange of references to the class object, and not the data values of these objects. The use of references in this exchange is effective in the case when an instance of a class (object) contains a large amount of data. After all, the exchange of two references is much faster than the exchange of all class data.

The following result will be displayed:

D[0]: value = 10; message value = 10
D[1]: value = 9; message value => 9
D[2]: value = 8; message value=8
D[3]: value = 7; message value = 7
D[4]: value = 7; message value = 7
D[5]: value = 6; message value => 6
D[6]: value = 5; message value = 5
D[7]: value = 4; message value = 4
D[8]: value = 3; message value = 3
D[9]: value = 2; message value = two

If we implement data exchange in a class, the following sorting loop code will be longer than the previous one:

// This code runs more slowly
String msg;
int val;
for (int i=0; i<D.length-1; i++)
    for (int j=i; j>=0; j--)
        if (D[j].value<D[j+1].value) {
            // swapping of these objects is slower
            val = D[j].value;
            D[j].value = D[j+1].value;
            D[j+1].value = val;
            msg = D[j].message;
            D[j].message = D[j+1].message;
            D[j+1].message = msg;
        }

6. Is it possible to assign objects of different classes?

No, it is not. When assigning objects of different classes, the Java compiler generates an error with the message:

Type mismatch: cannot convert from SomeClass to SomeClass2





7. How does the operation of assigning objects containing nested objects work?

 At assignment of one object to other value of the reference is assigned. An object declared in a class is not affected by the assignment operation.

8. Object comparison operation ==. Example

When comparing objects, reference values are compared. The result of the comparison is true if the references have the same values, that is, they refer to the same object.

Let the DayWeek class be given to implement the day of the week.

// class DayWeek
class DayWeek {
    int day;

    DayWeek(int day) {
        if ((day>=1)&&(day<=7))
        this.day = day;
    }
}

The following example compares objects of a class of type DayWeek. The result is set to true only when the values of the references to the objects of the class are equal.

DayWeek dw = new DayWeek(5);
DayWeek dw2= null;
boolean b;

b = dw==dw2; // b = false
dw2 = dw; // dw2 and dw are equal
b = dw==dw2; // b = true

// references to different memory areas
DayWeek dw3 = new DayWeek(3);
DayWeek dw4 = new DayWeek(3);

if (dw3==dw4)
    b = true;
else
    b = false; // b = false

9. Operation of object comparison !=. Example

Operation != (Not equal) returns false in the case when the value of references to class objects are different among themselves. The operation works just like the comparison operation for equality == of class objects.

Let the class Circle be given, which implements a circle on the coordinate plane

// a class that implements the circle
class Circle {
    double x, y;
    double radius;

    // constructor
    Circle() {
        radius = 1;
        x = y = 0;
    }
}

The following demonstrates how to use the != operation to compare objects.

// declaration of objects of Circle class
Circle c1 = new Circle(); // c1 - reference to the object of class Circle
Circle c2 = new Circle(); // c2 - reference to the object of class Circle
c2.radius = 2.5;

boolean b;
b = c1!=c2; // b = true: c1 and c2 references have different values
c2 = c1;
b = c1!=c2; // b = false: references c1, c2 have the same values


Related topics