BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Assignment Operators in Java with Examples

Last Updated: October 15, 2022 by Chaitanya Singh | Filed Under: java

Operator is a symbol that instructs the compiler to perform a specific action. For example, a “+” operator instructs the compiler to perform addition, a “>” operator instructs the compiler to perform comparison, “=” for assignment and so on. The operators in java are classified in eight different categories. In this guide, we will mainly discuss Assignment operators in Java.

In any operation, there is an operator and operands. For example: In a+b, the “+” symbol is the operator and a & b are operands.

Assignment Operator in Java

Assignment Operators List

The following assignment operators are supported in Java.

The associativity of assignment operator is “right to left”, which means the when compiler encounters assignment operator, it starts to evaluate the expression from right to left.

For example:

int num = 10;
The compiler assigns the value 10 to the variable num.

1. = Operator example

This is the simple assignment operator. The right side value is assigned to the operand on the left side.

public class JavaExample {
  public static void main(String[] args)
  {
    int n; //integer variable
    char ch; //character variable
    String str; //string variable

    // Simple assignment operator to assign values to variables
    n = 1;
    ch = 'A';
    str = "BeginnersBook.com";

    // Displaying the values of all the variables
    System.out.println("The value assigned to 'n': " + n);
    System.out.println("The value assigned to 'ch': " + ch);
    System.out.println("The value assigned to 'str': " + str);
  }
}

Output:

Output simple assignment operator

2. += Operator example

This is a combination of + and = operators. a += b; is equivalent to a = a+b;

This means the right side value is added to the left side value and the result is assigned to the left side operand.

public class JavaExample {
  public static void main(String[] args)
  {
    //integer variables
    int n1=1, n2=2, n3=3;

    //+= operator is used here to increase the value of variables
    //by the number mentioned in the right side of operator
    n1+=10;
    n2+=5;
    n3+=100;

    System.out.println(n1);
    System.out.println(n2);
    System.out.println(n3);
  }
}

Output:

11
7
103

3. -= Operator example

This is a combination of – and = operators. a -= b; is equivalent to a = a-b;
The value on the right side is subtracted from the left side variable’s value and the result is assigned back to the variable on left side.

public class JavaExample {
  public static void main(String[] args)
  {
    //integer variables
    int num1=100, num2=200, num3=300;

    //-= operator is used here to increase the value of variables
    //by the number mentioned in the right side of operator
    num1-=50;
    num2-=100;
    num3-=200;

    System.out.println(num1);
    System.out.println(num2);
    System.out.println(num3);
  }
}

Output:

50
100
100

4. *= Operator example

It is a combination of * and = operators. The value on the right side is multiplied to the left side variable’s value and the result is assigned back to the left side variable. a *= b; is equivalent to a = a*b;.

public class JavaExample {
  public static void main(String[] args)
  {
    //declaring integer variables
    int num1=10, num2 =20;

    //Operator *= used here to multiply the num1 by num2
    //and assign the result to num1
    num1 *=num2;

    System.out.println(num1);
    System.out.println(num2);
  }
}

Output:

200
20

5. /= Operator example

It is a combination of / and = operators. The compiler divides the left side variable’s value by the right side value and the quotient is assigned back to the left side variable. a /= b; is equivalent to a = a/b;.

public class JavaExample {
  public static void main(String[] args)
  {
    //declaring integer variables
    int x=100, y =20;

    //Operator /= used here to divide the variable x by
    // variable y and assign the quotient to x
    x /=y;

    System.out.println("Value of variable x: "+x);
    System.out.println("Value of variable y: "+y);
  }
}

Output:

Output of /= operator

6. %= Operator example

It is a combination of % and = operators. The compiler divides the left side variable’s value by the right side value and the remainder is assigned back to the left side variable. a %= b; is equivalent to a = a%b;.

public class JavaExample {
  public static void main(String[] args)
  {
    //declaring integer variables
    int x=100, y =20;

    //Operator %= used here to divide the variable x by
    // variable y and assign the remainder to x
    //dividing 100 by 20 gives quotient value as 5 and
    // remainder value as 0 as it is perfectly divisible
    x %=y;

    System.out.println("Value of variable x: "+x);
    System.out.println("Value of variable y: "+y);
  }
}

Output:

output %= operator

Assignment operator in For loop

Assignment operator is used in for loop as well.

class JavaExample {
  public static void main(String[] args) {
    int n = 10;
    // The first expression of the for loop is
    // an assignment operation where the value
    // of the loop variable is initialized
    for (int i = 1; i <= n; ++i) {
      System.out.print(i+ " ");
    }
  }
}

Output:

1 2 3 4 5 6 7 8 9 10

Frequently asked Questions on Assignment operators:
Is it possible to do Assignment operator Overloading?
No, Java doesn’t support operator overloading. C++ allows operator overloading. This is covered in the C++ vs Java post.

What does an assignment return in Java?
An assignment operator return the value specified by the left operand after the assignment. The type of the return value is the type of the left operand. For Example:

public class JavaExample {
  public static void main(String[] args)
  {
    int num;
    if(10==(num=10)){
      System.out.println("The value of num is: "+num);
    }
  }
}

Output:

The value of num is: 10

As you can see, In the above example, we are using assignment operator in if statement. We did a comparison of value 10 to an assignment operator which resulted in a ‘true’ output because the return of assignment operator is the value of left operand.

Recommended Posts

  • Arithmetic Operators in Java with Examples
  • Unary Operators in Java with Examples
  • Logical Operators in Java with Examples
  • Relational Operators in Java with Examples
  • Bitwise Operators in Java with Examples
  • Ternary Operator in Java with Examples
  • Shift Operators in Java with Examples
❮ Operators in Java

Top Related Articles:

  1. Java Variables: Declaration, Scope, and Naming Conventions
  2. Unary Operators in Java with Examples
  3. Can Static Methods be Overloaded or Overridden in Java?
  4. Logical Operators in Java with Examples
  5. Relational Operators in Java with Examples

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap