The article directories

  • The assignment operator
    • 1. Introduction
    • 2. Classification of assignment operators
    • 3. Case demonstration
    • 4. Assignment operator characteristics

The assignment operator

1. Introduction

  • An assignment operator assigns the value of an operation to a specified variable.

2. Classification of assignment operators

  • The basic assignment operator =,int a = 10;
  • The compound assignment operator

    +=, -=, *=, /=, %=And so on+ =Other uses are the same
a += b; Equivalent a = a + b;  a -= b; Equivalent a = a - b;Copy the code

3. Case demonstration

  • This example demonstrates the basic use of the assignment operator.AssignOperator.java
  1. Basic case of assignment [int num1 = 10]
  2. + =Use cases of
	int n1 = 10;
	n1 += 4;// n1 = n1 + 4;
	System.out.println(n1); / / 14
	n1 /= 3;// n1 = n1 / 3; / / 4
	System.out.println(n1); / / 4
Copy the code

4. Assignment operator characteristics

  1. It goes from right to leftint num = a + b + c;
  2. The left-hand side of an assignment operator can only be variables, and the right-hand side can be variables, expressions, and constant values
int num = 20; int num2= 78 * 34 - 10; int num3 = a;
Copy the code
  1. The compound assignment operator is equivalent to the following effect

    Such as:a+=3;Is equivalent toa=a+3;Other analogy
  2. The compound assignment operator performs type conversions.
	// The compound assignment operator performs type conversions
	byte b = 3;
	b += 2; // equivalent b = (byte)(b + 2);
	b++; // b = (byte)(b+1);
Copy the code