Java. Assignment operation. The ternary operation ?:. Priority of operations




Assignment operation. The ternary operation ?:. Priority of operations


Contents


Search other websites:

1. What is the purpose of assignment operation? Examples

Assignment operation denoted by the symbol “=“. The assignment operation is one of the most used in programs. Operation of assignment is intended to assign a value to a variable.

General view of assignment operation:

variable = expression;

The assignment operation functions as follows: first, is calculated value of the expression on the right side, then this value is assigned to the variable, which is located on the left side of the sign ‘=‘.

When assigning, the type of variable and expression must be compatible. More about the type conversion in expressions is described here.

The assignment operation can take the form of a chain.

For example, it may be a chain of two assignment operations:

variable1 = variable2 = expression;

In this case, is performed the following algorithm:

  • at first, the value of expression is calculated;
  • result of the expression is assigned to variable2;
  • result of the expression is assigned to variable1.

The chain (sequence) of the assignment operations can contain any number of assignments.

Example of the assignment operation.

// assignment operation
int a;
double x, y, z;

x = -5.88; 
y = z = x; // y = -5.88, z = -5.88
x = y = z = 3.9; // x = 3.9, y = 3.9, z = 3.9

// assignment and types casting
x = -8.6;
a = (int)x; // a = -8
y = a; // y = -8.0

char c = 'T';
int code = (int)c; // code = 84 - the code of character 'T'

// logical type
boolean b;
b = false && true; // b = false

2. What is the general form of a ternary operation ‘? :‘ ?

Ternary operation ? : replaces construction

if ... then ... else

The general form of operation

expression1 ? expression2 : expression3

In the above form the expression1 – any logical expression (boolean), the result of which is the value of true or false. If the expression value of expression1 is true, then expression2 is calculated, otherwise calculated expression3.



3. Examples of the use of a ternary operation ? :

Example 1. Using the ternary operator ? : to search for the maximum between two numbers. A fragment of program code.

// ternary operation ?:
double a, b;
double max; // maximum

// inputting the values of a and b
// ...

// search the maximum between two numbers
max = a>b ? a : b;

Example 2. Using the ternary operator ?: for searching the maximum value of between the three numbers a, b, c.

// ternary operation ?:
double a, b, c;
double max; // maximum

// inputting the values of a, b, c
// ...

// Search a maximum between three numbers
max = a>b ? a : b;
max = max>c ? max : c;

4. How does the assignment operation work for objects? Example

In the Java programming language, an assignment operation can be applied to objects. The assignment for objects differs from the assignment of primitive types variables.

When assigning primitive types, a simple copying from one memory location (right side of an assignment statement) to another space (left side) takes place. If to write

int x, y;
y = 5;
x = y;

then when executing the line

x = y;

the value 5 specified in the variable y will be copied into the variable x. After that, changing the value in one variable will not affect the value in the other variable, because the variables of primitive types x, y are located in different parts of the memory.

In the case of the assignment of objects, the object references themselves are assigned, and not the internal data of these objects. That is, after assignment, these links will point to the same memory location (as opposed to assigning variables of primitive types).

Example. Let class A be defined, in which one internal variable a and class constructor are defined

class A {
    int a;

    // class constructor
    A() {
        a = 10;
    }
}

The following program code shows the result after the assignment operation

// declaring and allocating memory for objects of class A
A obj1 = new A();
A obj2 = new A();
int d;

// constructor test
d = obj1.a; // d = 10
d = obj2.a; // d = 10

// set a new and different values
obj1.a = 30;
obj2.a = 28;

// before assignment statement
d = obj1.a; // d = 30
d = obj2.a; // d = 28

// assignment of objects
obj2 = obj1;

// after assignment obj1 and obj2 refer a common memory location (obj1)
d = obj1.a; // d = 30
d = obj2.a; // d = 30

// changing the value in one of the objects
obj2.a = 77;

// access by another reference (obj2) - the same value
d = obj1.a; // d = 77

As can be seen from the above code, after assigning objects, these objects refer to the same memory location. In this case, it is a section of memory allocated for the object obj1. The section of memory that was allocated for the object obj2 after the assignment operation is irreversibly lost. Later this location will be cleared by the “garbage collector”.

After assignment, changing the data in the obj1 object will result in a change in the data in obj2, since these references refer to the same memory location.






5. Table of priorities of operations
The priority of operations in descending order
--------------------------------------------------------------
++ --(postfix)
++ --(prefix) ~ ! +(unary) -(unary) (types cast)
*   /   %
+   -
>>  >>>  <<
>   >=   <    <=    instanceof
==  !=
&
^
|
&&
||
?:
->
=  +=  -=  *=  /=  %=  >>=  <<=  >>>=


Related topics