Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Assignment operator (=)

The `=` operator stores a value in a variable; it does not test equality. It requires compatible types, and the assignment expression itself also produces a value, allowing assignments to be chained.

Version: Java SE 21; repository runtime, production runtime not attested

Syntax

variable = expression

The = operator stores the right-hand value in the variable designated on the left: a local variable, field, or array element. == compares instead of assigning. In the declaration int count = 2;, = introduces an initializer.

The right-hand side must be compatible with the target type. Specific implicit conversions are allowed, such as int to long; double to int requires an explicit cast. The constant in byte b = 10; is a permitted special case of narrowing.

An assignment produces the assigned value. other = count = 7 groups as other = (count = 7). A final variable can be assigned only when it is definitely unassigned.

Assignment copies a value, not a live link between variables. For references, it copies the reference, not the object. In the example, snapshot retains 2 despite later changes to count.

Examples

public class Main {
    public static void main(String[] args) {
        int count = 2;
        int snapshot = count;
        count = count + 3;
        int other;
        other = count = 7;
        System.out.println(snapshot);
        System.out.println(count);
        System.out.println(other);
    }
}

Result

2
7
7

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources