Java assignment operators | Developer.com

Textbooks for Java programming

Java provides many types of operators to perform various calculations and functions, such as logical, arithmetic, relational, and others. With so many operators to choose from, it helps to group them based on the type of functionality they provide. This programming guide will focus on a number of Java aaward operators.

However, before we get started, you might want to bookmark our other guides on Java operators, which include:

Assignment operators in Java

As the name suggests, assignment operators are used to assign values ​​to variables using the following syntax:

variable operator value;

The left-hand side of the assignment operator’s operand must be a variable, while the right-hand side of the assignment operator’s operand can be a literal value or another variable. Moreover, the value or variable on the right must be of the same data type as the operand on the left. Otherwise, the compiler will throw an error. Assignment operators have right-to-left associativity in the sense that the value given on the right-hand side of the operator is assigned to the variable on the left-hand side. Therefore, the variable on the right must be declared before assignment.

You can learn more about variables in our programming guide: Working with Java variables.

Types of assignment operators in Java

Java assignment operators are classified into two types: easy and compound.

The Easy task operator is equal to (=), which is the simplest in the series. It simply assigns the value or variable on the right to the variable on the left.

Complex operators consist of arithmetic, bit operator or shift operator with equality (=) sign.

Equal operator (=) Java example

First, let’s learn to use one and only simple assignment operator – the Equally (=) operator – with the help of Java programs. It involves two assignments: a literal value for number 1 and number 1 variable for number 2after which both are printed to the console to show that the values ​​are assigned to numbers:

class EqualsOperatorExample 
  public static void main(String[] args) 
    int num1, num2;
    // Assigning a literal value to num1
    num1 = 5;
    System.out.println(num1); // 5
    // Assigning value of variable num1 (5) to num2
    num2 = num1;
    System.out.println(num2); // 5
  


An example of the Java operator +=

Compound of + and = operators, += adds the current value of the variable on the left to the value on the right before assigning the result to the operand on the left. Here is a sample code to demonstrate usage += operator in Java:

class CompoundAdditionOperatorExample 
  public static void main(String[] args)
  
    int num1 = 20, num2 = 30;
        
    System.out.println("num1 = " + num1); // num1 = 20
    System.out.println("num2 = " + num2); // num2 = 30
    
    // Adding and Assigning values
    num1 += num2;
    
    System.out.println("num1 = " + num1); // num1 = 50
  


Java example of the -= operator

Consists of and = operators, -= it first subtracts the value of the variable on the right from the current value of the variable on the left before assigning the result to the operand on the left. We can see it in action below in the following code example that shows how to reduce in Java using -= operator:

class CompoundSubtractionOperatorExample 
  public static void main(String[] args)
  
    int num1 = 20, num2 = 30;
    
    System.out.println("num1 = " + num1); // num1 = 20 
    System.out.println("num2 = " + num2); // num2 = 30
    
    // Subtracting and Assigning values
    num1 -= num2;

    System.out.println("num1 = " + num1); // num1 = -10
  


*= Operator Java example

This Java operator consists of * and = operators. It works by multiplying the current value of the variable on the left by the value on the right and then assigning the result to the operand on the left. Here is a program that shows *= operator in action:

class CompoundMultiplicationOperatorExample 
  public static void main(String[] args)
  
    int num1 = 20, num2 = 30;
    
    System.out.println("num1 = " + num1); // num1 = 20 
    System.out.println("num2 = " + num2); // num2 = 30
    
    // Multiplying and Assigning values
    num1 *= num2;
 
    // Displaying the assigned values
    System.out.println("num1 = " + num1); // num1 = 600
  


/= Operator Java example

Combination / and = operators, /= The operator divides the current value of the variable on the left by the value on the right and then appends the quotient to the operand on the left. Here are some code examples to show how to use /= operator in Java:

class CompoundDivisionOperatorExample 
  public static void main(String[] args)
  
    int num1 = 30, num2 = 20;
    
    System.out.println("num1 = " + num1); // num1 = 30 
    System.out.println("num2 = " + num2); // num2 = 20
    
    // Multiplying and Assigning values
    num1 /= num2;
    
    // Displaying the assigned values
    System.out.println("num1 = " + num1); // num1 = 1
  


%= An example of a Java operator

The %= operator includes and % and = operators. As seen in the program below, it divides the current value of the variable on the left by the value on the right and then assigns the remainder to the operand on the left:

class CompoundModulusOperatorExample 
  public static void main(String[] args)
  
    int num1 = 30, num2 = 20;
    
    System.out.println("num1 = " + num1); // num1 = 30 
    System.out.println("num2 = " + num2); // num2 = 20
    
    // Modulus and value assignment
    num1 %= num2;
    
    // Displaying the assigned values
    System.out.println("num1 = " + num1); // num1 = 10
  


Complex bit operators and shift operators in Java

The Bitwise and Shift operators we covered recently can also be used in complex form as seen in the list below:

  • &= – Complex bitwise assignment operator.
  • ^= – Complex bitwise ^ assignment operator.
  • >>= – Complex assignment operator right shift.
  • >>>= – Complex right shift filled with 0 assignment operator.
  • <<= – Complex left shift assignment operator.

The following program demonstrates the operation of all Complex Bitwise and Shift operators:

class CompoundBitwiseAndShiftOperatorsExample 
  public static void main(String args[]) 
    byte b1 = 127;
    b1 %= 7;
    byte b2 = 120;
    b2 &= 40;
    short s1 = 300;
    s1 ^= 100;
    byte b3 = 127;
    b3 >>= 3;
    short s2 = 100;
    s2 >>= 3;
    short s3 = 200;
    s3 >>>= 4;
    System.out.println("b1 = " + b1); // b1 = 1
    System.out.println("b2 = " + b2); // b2 = 40
    System.out.println("b3 = " + b3); // b3 = 15
    System.out.println("s1 = " + s1); // s1 = 328
    System.out.println("s2 = " + s2); // s2 = 800
    System.out.println("s3 = " + s3); // s3 = 12
  


Final thoughts on Java assignment operators

This programming guide presents an overview of simple and complex assignment operators in Java. An essential component of any programming language, programmers would not be able to store any data in their programs without them. Although not as essential as the equality operator, compound operators are great time savers, allowing you to perform arithmetic and bitwise operations and assignments in a single line of code.

Read more Java programming guides and software development guides.

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *