Assignment Operators, Increment/Decrement Operators

Assignment Operators, Increment/Decrement Operators

In the Java programming language, the assignment and increment/decrement operators are reserved symbols or special characters that help programmers perform multiple programmatic operations on program variables and their values. The assignment operator and increment/decrement operators are some of the most commonly used special operators in Java programs. They help programmers manipulate programmatic variables and their values. Using the assignment and increment/decrement operators in Java helps control the flow of multi-purpose program calculations.

Assignment Operators, IncrementDecrement Operators

Assignment Java Operator.

The assignment operator in Java is used to assign a value to a previously declared or new programmatic variable. The most common assignment operator in Java is the simple = equals operator. Java also provides users with a variety of compound assignment operators that allow programs to combine arithmetic, logical, and programmatic operations with assignment. Assignment operators make it easy to perform an operation on a variable or assign or declare a result value in the same step.

Simple Assignment (=) Operator.

The basic assignment operator in Java programs is used to assign a new int, float, double, or string value to a program-declared variable.

Simple Assignment (=) Operator Syntax.

variable data type = expression;

Example of the Simple Assignment (=) Operator.

int p = 7; // Here, the variable ‘p’ is assigned the value 7.

Compound Assignment Java Operator.

Compound assignment operators in Java programs merge arithmetic numeric data operations with the assignment operator. Compound assignment operators simplify program code tasks by eliminating variable repetition processes on both sides of the program.

List of popular Compound Assignment Operators

OperatorCompound Assignment Operators DescriptionCompound Operators Example
+=This is used to Adds the right-hand operand to the left-hand operand, then finally it assigns the result to the left operand value.p += q is equivalent to p = p + q
-=This operator used to Subtracts the right-hand operand from the left-hand operand, then it finally assigns the result to the left operand value.p -= q is equivalent to p = p – q
*=This operator used to Multiplies the left-hand operand by the right-hand operand, then finally it assigns the result to the left operand value.p *= q is equivalent to p = p * q
/=This operator used to Divides the left-hand operand by the right-hand operand, then finally it assigns the result to the left operand value.p /= q is equivalent to p = p / q
%=This operator used to find out Takes the modulus of the left-hand operand by the right-hand operand, then finally it assigns the result to the left operand value.p %= q is equivalent to p = p % q

Example of the Compound Assignment Operator.

public class Main

{

public static void main(String[] args) {

int p = 7, q = 4;

// here we simple assign q value to p

p=q; // p value is now 4

// let’s use simple compound assignment operators behavior

p += 4; // p = p + 4 -> p is now 8

p -= 3; // p = p – 3 -> p is now 5

p*=3; // p = p * 3 -> p is now 15

p /= 3; // p = p / 3 -> p is now 5 (integer division)

p %= 3; // p = p % 3 -> p is now remainder 2 (remainder of 5 / 3)

System.out.println(“Compound operator value is – ” + p); // Result is – 2

}

}

Increment and Decrement Java Operators.

In any Java program, the increment and decrement operators are used to increase or decrease the value of a variable by one. The increment and decrement operators are most commonly used in programming looping condition expressions. For example, if/else statements, for, while, and do-while loops update the value of a variable in the loop counter.

Increment Java Operator (++).

In Java programs, the declared increment operator increases the existing value of a variable by 1. The increment operator can be used in two ways.

Element of Increment Java Operator (++).

  • Post-increment (p++) – Here, the declared variable p is increased by 1, but here it returns the original value of the variable before the increment.
  • Pre-increment (++p) – This method increments the value of the variable p declared here by 1 and provides the updated value as the output result.

Java Post-increment syntax.

Post-increment Java operator.

p++;

Pre-increment Java operator.

++p;

Example of the increment Java operator.

public class Main

{

public static void main(String[] args) {

int p = 7;

// Here we implement Post-increment. It displays the original variable value and then applies increments.

System.out.println(p++); // Result is – 7. It prints the original value of p before incrementing.

System.out.println(p); // Result is – 8 after incrementing the above value with 1.

/ Here we implement Pre-increment. It increments the first variable and then returns the updated variable value.

System.out.println(++p); // Result is – 9 after incrementing the above value with 1

}

}

Explanation of the Increment Java Operator.

  • Post-increment (p++) – Here the value of the variable p is first used in the program, and then the value of the variable p is incremented.
  • Pre-increment (++p) – Here the value of p is first incremented in the program, and then the new value is used.

Decrement Java Operator (–).

In a Java program, the decrement operator decrements the current value of a declared variable by 1. A Java programmer can use the decrement operator in two ways.

Element of Decrement Java Operator (–).

  • Post-decrement (p –) – It decrements the value of the declared variable p by 1 in the Java program, but displays the variable’s original value before decrement.
  • Pre-decrement (–p) – It decrements the value of the declared variable p by 1 in the Java program, and provides the updated value as the result.

Syntax of the Decrement Java Operator (–).

Post-decrement operator.

p–;

Pre-decrement operator.

-p;

Example of Decrement Java Operator (–).

public class Main

{

public static void main(String[] args) {

int q = 9;

// here we implement Post-decrement, it returns original variable value, then decrements it

System.out.println(q–); // Result is – 9 here it prints variable value before decrementing

System.out.println(q); // Result is – 8 here it after decrement variable value

// here we implement Pre-decrement, it decrements first value, then returns the updated variable value

System.out.println(–q); // Result is – 7 (here the value is decremented after decrementing

}

}

Explanation of the Decrement Java Operator (–).

  • Post-decrement (q–) – Here the value of the variable q is first used in the Java program, and then the value of the variable q is decremented.
  • Pre-decrement (–q) – Here the value of the variable q is first decremented, and then the new variable value is used.

How to use the assignment and increment/decrement operators in a Java program.

Assignment operator usage.

Assignment operators in Java programs are used to initialize or update the declared variable values ​​in the program.

Compound assignment operators help make Java program source code clearer and smaller.

Increment/decrement operators.

Updating a counter or index value in a Java program is commonly done using if else block statements or for, do, while, they are used in while loops.

They help create multiple program condition expressions to count program variable values ​​or control variable value repetition in a loop.

Detail explanation of assignment or increment/decrement Operators

OperatorOperator DescriptionOperator ExampleRealtime program Behavior
=It is a Basic assignment operatorp = 7It Assigns the value 7 to variable p.
+=It Adds and assigns the resultp += 4It Equivalent to p = p + 4.
-=It used to Subtracts and assigns the resultp -= 7It is Equivalent to p = p – 7.
*=It used to Multiplies and assigns the resultp *= 8It is Equivalent to p = p * 8.
/=It used to Divides and assigns the resultp /= 9It is Equivalent to p = p / 9.
%=It displays Modulus and assigns the resultp %= 7It Equivalent to p = p % 7.
++The Increment operator used to (adds 1)p++ or ++pBoth condition it Increments p by 1 value.
The Decrement operator used to (subtracts 1)q– or –qIn Both condition it Decrements q by 1.

Conclusion on Assignment Operators, Increment/Decrement Operators.

  • Assignment operators help Java programmers assign variable values ​​properly, and using compound assignment operators simplifies program operations that repeat multiple times.
  • Increment and decrement operators in Java programs help increase or decrease the value of a variable by 1, making them very useful in user-defined loops and conditional expressions in programs.

Leave a Reply