Java Operators: Assignment, Arithmetic And Unary

This post shows the assignment, arithmetic and unary operators available in Java.

Assignment operator in Java

Assignment operator in Java is the “=”. It assigns the value on its right to the operand on its left. For example-

int i = 0;
double amount = 67.85;

Assignment operator can also be used on objects to assign object references. For example-

Person per = new Person();
Square square = new Square(5);

Arithmetic operators in Java

The arithmetic operators available in Java for basic mathematical operations are addition (+), subtraction (-), division (/), multiplication(*) and modulus (%). Modulus operator divides one operand by another and returns the remainder as its result.

Operator Description
+ Additive operator (also used for String concatenation)
Subtraction operator
* Multiplication operator
/ Division operator
% Remainder operator

Java arithmetic operators example

public class JavaOperators {
  public static void main(String[] args) {
    int a = 5;
    int b = 7;
    
    //Addition
    int result = a + b;
    System.out.println("a + b = " + result);
    
    // Subtraction
    result = b - a;
    System.out.println("b - a = " + result);
    
    // Multiplication
    result = a * b;
    System.out.println("a * b = " + result);
    
    // Division
    result = a/2;
    System.out.println("a/2 = " + result);
    
    // Modulus
    result = b%5;
    System.out.println("b%5 = " + result);
  }
}

Output

a + b = 12
b - a = 2
a * b = 35
a/2 = 2
b%5 = 2

Overloaded + operator for String

The + operator is overloaded in Java and it can also be used for concatenating (joining) two strings together.

public class JavaOperators {
  public static void main(String[] args) {
    String str1 = "Hello";
    String str2 = "World";
    String joinStr = str1 + str2;
    System.out.println("Concatenated String- " + joinStr);
  }
}

Output

Concatenated String- HelloWorld

Compound assignment in Java

You can also combine the arithmetic operators with the assignment operator to create compound assignments in Java. For example x*=5; is equal to x = x * 5;

Java Compound assignment example

public class JavaOperators {

  public static void main(String[] args) {
    int a = 5;
    int b = 7;
    
    a+=1;
    System.out.println("a = " + a);
    
    // Subtraction
    b-=2;
    System.out.println("b  = " + b);
    
    // Multiplication
    a*=4;
    System.out.println("a = " + a);
    
    // Division
    b/=3;
    System.out.println("b = " + b);
    
    // Modulus
    a%=5;
    System.out.println("a = " + a);
  }
}

Output

a = 6
b  = 5
a = 24
b = 1
a = 4

Unary operators in Java

The unary operators in Java require only one operand; they perform various operations such as incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean.

Operator Description
+ Unary plus operator; indicates positive value (numbers are positive without this, however)
Unary minus operator; negates an expression
++ Increment operator; increments a value by 1
Decrement operator; decrements a value by 1
! Logical complement operator; inverts the value of a boolean

Java Unary operators example

public class JavaOperators {

  public static void main(String[] args) {
    // minus 
    int a = -7;
    System.out.println("a = " + a);
    // plus (default so a = 7 also ok)
    a = +7;
    System.out.println("a = " + a);
    // increment operator
    a++;
    System.out.println("a = " + a);
    // decrement operator
    a--;
    System.out.println("a = " + a);
    boolean flag = false;
    // logical complement (inverting value of boolean)
    System.out.println("flag is " + !flag);
  }
}

Output

a = -7
a = 7
a = 8
a = 7
flag is true

Increment and Decrement operators in Java

Unary operators increment and decrement operators in Java are special as they can be applied after the operand (postfix) or before the operand (prefix) i.e. a++; and ++a; both are valid and result in incrementing the value of a by 1.

Difference between these two forms is that the prefix version (++a) evaluates to the incremented value, whereas the postfix version (a++) evaluates to the original value and then incremented. If you are using this operator in part of a larger expression or assigning it to another variable then which form is chosen may make a difference.

public class JavaOperators {
  public static void main(String[] args) {
    int a = 7;
    int b = a++;
    System.out.println("b = " + b);
    System.out.println("a = " + a);
  }
}

Output

b = 7
a = 8

As you can see here a++ evaluates to the original value which is assigned to b and then a is incremented.

public class JavaOperators {
  public static void main(String[] args) {
    int a = 7;
    int b = ++a;
    System.out.println("b = " + b);
    System.out.println("a = " + a);
  }
}

Output

b = 8
a = 8

Here ++a; evaluates to the incremented value which is assigned to b.

Related Posts

That’s all for the topic Java Operators: Assignment, Arithmetic And Unary. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.