Data Types and Operators
(Non-Primitive Data Types)
Control Flow Statements
Conditional Statements
Looping Statements
Branching Statements
Object-Oriented Programming (OOP)
Exception Handling
Collections Framework
Overview of Collections
Java I/O
Multithreading
GUI Programming with Swing
Advanced Topics
JAVA CODE
Java Basics
Working with Objects
Arrays, Conditionals, and Loops
Creating Classes and Applications in Java
More About Methods
Java Applet Basics
Graphics, Fonts, and Color
Simple Animation and Threads
More Animation, Images, and Sound
Managing Simple Events and Interactivity
Creating User Interfaces with the awt
Windows, Networking, and Other Tidbits
Modifiers, Access Control, and Class Design
Packages and Interfaces
Exceptions
Multithreading
Streams and I/O
Using Native Methods and Libraries
Under the Hood
Java Programming Tools
Working with Data Structures in Java
Advanced Animation and Media
Fun with Image Filters
Client/Server Networking in Java
Emerging Technologies
appendix A :- Language Summary
appendix B :- Class Hierarchy Diagrams
appendix C The Java Class Library
appendix D Bytecodes Reference
appendix E java.applet Package Reference
appendix F java.awt Package Reference
appendix G java.awt.image Package Reference
appendix H java.awt.peer Package Reference
appendix I java.io Package Reference
appendix J java.lang Package Reference
appendix K java.net Package Reference
appendix L java.util Package Reference

In Java, assignment operators are used to assign values to variables. They are an essential part of Java programming, enabling developers to store values, perform arithmetic operations, and update variables in a concise and readable manner. Here’s a detailed description of the various assignment operators in Java, along with examples:

Basic Assignment Operator (=)

The simplest assignment operator is the equal sign (=). It assigns the value on the right to the variable on the left.

				
					int a = 10; // Assigns the value 10 to variable a

				
			

Compound Assignment Operators

Java also provides several compound assignment operators, which are shorthand for performing an operation and assigning the result to a variable.

1. Addition Assignment (+=)

Adds the right operand to the left operand and assigns the result to the left operand.

				
					int a = 10;
a += 5; // Equivalent to a = a + 5
System.out.println(a); // Outputs 15

				
			

2. Subtraction Assignment (-=)

Subtracts the right operand from the left operand and assigns the result to the left operand.

				
					int a = 10;
a -= 3; // Equivalent to a = a - 3
System.out.println(a); // Outputs 7

				
			

3. Multiplication Assignment (*=)

Multiplies the left operand by the right operand and assigns the result to the left operand.

				
					int a = 10;
a *= 2; // Equivalent to a = a * 2
System.out.println(a); // Outputs 20

				
			

4. Division Assignment (/=)

Divides the left operand by the right operand and assigns the result to the left operand.

				
					int a = 10;
a /= 2; // Equivalent to a = a / 2
System.out.println(a); // Outputs 5

				
			

5. Modulus Assignment (%=)

Computes the remainder of dividing the left operand by the right operand and assigns the result to the left operand.

				
					int a = 10;
a %= 3; // Equivalent to a = a % 3
System.out.println(a); // Outputs 1

				
			

Bitwise Assignment Operators

These operators perform a bitwise operation and assign the result to the left operand.

1. Bitwise AND Assignment (&=)

Performs a bitwise AND operation and assigns the result to the left operand.

				
					int a = 10; // 1010 in binary
a &= 7;     // 0111 in binary, result is 2 (0010 in binary)
System.out.println(a); // Outputs 2

				
			

2. Bitwise OR Assignment (|=)

Performs a bitwise OR operation and assigns the result to the left operand.

				
					int a = 10; // 1010 in binary
a |= 5;     // 0101 in binary, result is 15 (1111 in binary)
System.out.println(a); // Outputs 15

				
			

3. Bitwise XOR Assignment (^=)

Performs a bitwise XOR operation and assigns the result to the left operand.

				
					int a = 10; // 1010 in binary
a ^= 3;     // 0011 in binary, result is 9 (1001 in binary)
System.out.println(a); // Outputs 9

				
			

4. Left Shift Assignment (<<=)

Shifts the bits of the left operand to the left by the number of positions specified by the right operand and assigns the result to the left operand.

				
					int a = 10; // 1010 in binary
a <<= 2;    // Shift left by 2 positions, result is 40 (101000 in binary)
System.out.println(a); // Outputs 40

				
			

5. Right Shift Assignment (>>=)

Shifts the bits of the left operand to the right by the number of positions specified by the right operand and assigns the result to the left operand.

				
					int a = 10; // 1010 in binary
a >>= 2;    // Shift right by 2 positions, result is 2 (0010 in binary)
System.out.println(a); // Outputs 2

				
			

6. Unsigned Right Shift Assignment (>>>=)

Shifts the bits of the left operand to the right by the number of positions specified by the right operand, filling the leftmost bits with zero, and assigns the result to the left operand.

				
					int a = -10; // 11111111111111111111111111110110 in binary (32-bit signed integer)
a >>>= 2;    // Unsigned shift right by 2 positions, result is 1073741821 (00111111111111111111111111111101 in binary)
System.out.println(a); // Outputs 1073741821

				
			

Example Program

Here’s a full example program that demonstrates the use of various assignment operators in Java:

				
					public class AssignmentOperatorsDemo {
    public static void main(String[] args) {
        int a = 10;
        int b = 5;

        // Basic assignment
        int result = a;
        System.out.println("result = " + result); // 10

        // Addition assignment
        result += b;
        System.out.println("result += b -> " + result); // 15

        // Subtraction assignment
        result -= b;
        System.out.println("result -= b -> " + result); // 10

        // Multiplication assignment
        result *= b;
        System.out.println("result *= b -> " + result); // 50

        // Division assignment
        result /= b;
        System.out.println("result /= b -> " + result); // 10

        // Modulus assignment
        result %= b;
        System.out.println("result %= b -> " + result); // 0

        // Bitwise AND assignment
        result = a;
        result &= b;
        System.out.println("result &= b -> " + result); // 0

        // Bitwise OR assignment
        result = a;
        result |= b;
        System.out.println("result |= b -> " + result); // 15

        // Bitwise XOR assignment
        result = a;
        result ^= b;
        System.out.println("result ^= b -> " + result); // 15

        // Left shift assignment
        result = a;
        result <<= 2;
        System.out.println("result <<= 2 -> " + result); // 40

        // Right shift assignment
        result = a;
        result >>= 2;
        System.out.println("result >>= 2 -> " + result); // 2

        // Unsigned right shift assignment
        result = -10;
        result >>>= 2;
        System.out.println("result >>>= 2 -> " + result); // 1073741821
    }
}

				
			

This program demonstrates the use of various assignment operators and prints the results of each operation. By running this code, you can see how each operator works and the impact it has on the variable values.

Scroll to Top