JAVA – Arithmetic Operators


The Java programming language provides operators that perform simple mathematical operations such as addition, subtraction, multiplication, and division. These operators resemble the basic mathematics symbols that we all are aware of. Unlike the normal mathematical symbols, the “%”operator divides one operand by another and returns a remainder in the result.

The Java programming languages uses the following symbols:

  • + Additive operator: This operator is not only used for addition, but is also used for String concatenation purposes.
  • - Subtraction operator: This operator is used for subtracting the operands.
  • * Multiplication operator:  This operator is used for multiplying the operands.
  • / Division operator: This operator is used for dividing two or more operands.
  • % remainder operator: This operator is used for obtaining the remainder by dividing one operand with another.

Let’s now look at a small program, Demo_of_operator_program, which shows the use of different arithmetic operators.

class Demo_of_operator_program {

public static void main (String[] args){

int result_operator = 20 + 42; // The result_operator is now 62

System.out.println(result_operator);

result_operator = result_operator – 22; // The result_operator is now 40

System.out.println(result_operator);

result_operator = result_operator * 3; // The result_operator is now 120

System.out.println(result_operator);

result_operator = result_operator / 2; // The result_operator is now 60

System.out.println(result_operator);

result_operator = result_operator + 10; // The result_operator is now 70

result_operator = result % 14; // The result_operator is now 5

System.out.println(result_operator);

}

}

The arithmetic operators listed above can be combined with the simple assignment operator to create compound assignments. For example the compound assignments such as, y+=1; and y=y+1; both increment the value of y by 1.

Not only this, you can also use the + or the additive operator for joining or concatenating two strings together. The Demo_of_Concat_Stings program displayed below displays a sample of string concatenation:

class Demo_of_Concat_Stings {

public static void main(String[] args){

String first_normal_String = “This is an”;

String second_normal_String = ” arithmetic operator”;

String third_concataneted_String = first_normal_String+second_normal_String;

System.out.println(third_concataneted_String);

}

}

This program displays the result: “This is an arithmetic operator.” which gets stored in the variable third_concataneted_String.

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>