Java arithmetic operators



Arithmetic Operators in Java Programming

  • The basic arithmetic operations in Java Programming are addition, subtraction, multiplication, and division.
  • Arithmetic Operations are operated on Numeric Data Types as expected.
  • Arithmetic Operators can be Overloaded.
  • Arithmetic Operators are “Binary” Operators i.e they operates on two operands.

Arithmetic Operators used in Java :

Operator Use of Operator
+ Use to Add Two Numbers and Also used to Concatenate two strings
- Used for Subtraction
* Used to multiply numbers
/ Used for Division
% Used for Finding Mod (Remainder Operator)

Live Example 1 : Arithmetic Operators

class ArithmeticOperationsDemo {

    public static void main (String[] args){

        // answer is now 3
        int answer = 1 + 2;
        System.out.println(answer);

        // answer is now 2
        answer = answer - 1;
        System.out.println(answer);

        // answer is now 4
        answer = answer * 2;
        System.out.println(answer);

        // answer is now 2
        answer = answer / 2;
        System.out.println(answer);

        // answer is now 10
        answer = answer + 8;

        // answer is now 3
        answer = answer % 7;
        System.out.println(answer);
    }
}

Live Example 2 : Use of Modulus Operator

class ModulusOperatorDemo {
  public static void main(String args[]) {
    int    x = 92;
    double y = 92.25;

    System.out.println("x mod 10 = " + x % 10);
    System.out.println("y mod 10 = " + y % 10);
  }
}

Output :

x mod 10 = 2
y mod 10 = 2.25

Live Example 3 : Joining Two Strings

class AssignmentConcatDemo {
    public static void main(String[] args){

      String firstName = "Pritesh";
      String lastName  = " Taral";

      String fullName  = firstName + lastname;
      System.out.println(fullname);
    }
}

Output :

Pritesh Taral

Other Operators : Assignment Operator | Compound Assignment