Wednesday, June 8, 2016

Arithmetic Operator

Arithmetic operator is operator that used to perform mathematical calculation such as addition, subtraction, multiplication, and division. The following table shows a list of operator that belong to the group of arithmetic operator:

  Operator  Remarks
  +   Addition
  -   Subtraction
  *   Multiplication
  /   Division
  %   Modulus (remainder of division)
  ++   Increment (increase the value by 1)
  --   Decrement (decrease the value by 1)

Operand of arithmetic operator must be numeric type. You can not use it to boolean data type. As an exception, for the char type, you can still perform arithmetic operations.

Here is an example of an arithmetic operation:

int a = 2, b = 4;
b = b + a;


Equals (=) Operator 


Called as assignment operator, which is used to enter a value into a variable. In the first line of code above, we insert the value 2 to the variable a, and the value 4 into the variable b.

Now try to consider the second line of code above. The code means that the new value of b is the old value of b (4) plus the value of a (2), so the result is 6. Same as in C/C ++, in Java, this such as statement can also be shortened to as following:

b += a;   //Same with b = b + a

Here is a table showing a list of operator in Java shorthand for assignment process or the process of entering a value into a variable:

  Operator   Remarks
  +=   Assignment for addition
  -=   Assignment for subtraction
  *=   Assignment for multiplication
  /=   Assignment for division
  %=   Assignment for modulus

Here is an example of its use:

b -= a;   //Same with b = b - a
b *= a;   //Same with b = b * a
b /= a;   //Same with b = b / a
b %= a;   //Same with b = b % a


Basic Arithmetic Operators


Basic arithmetic operators are: addition, subtraction, multiplication, and division. Basic arithmetic operators are operators that you need in numerical computation processes in general. Operator minus (-) also can be used to negate a single operand.

When we work with the division operator (/), if both operands is integer, then the result would be an integer. However, if one or both of its operands is floating-point, the result to be obtained would be floating-point.

Here is an example of a program that will demonstrate the use of basic arithmetic operators, both for the integer or floating-point:

class BasicArithmeticDemo {

   public static void main(String[] args) {

      System.out.println("Arithmetic operation for integer type");

      int a = 2 + 1;
      int b = a - 1;
      int c = a * b;
      int d = c / 3;
      int e = -a;

      System.out.println("Value of a: “ + a);
      System.out.println("Value of b: “ + b);
      System.out.println("Value of c: “ + c);
      System.out.println("Value of d: “ + d);
      System.out.println("Value of e: “ + e);
      System.out.println();

      System.out.println ("Arithmetic operation for floating-point type");

      double fa = 2 + 1;
      double fb = fa - 1;
      double fc = fa * fb;
      double fd = fc / 3;
      double fe = -a;

      System.out.println("Value of fa: “ + fa);
      System.out.println("Value of fb: “ + fb);
      System.out.println("Value of fc: “ + fc);
      System.out.println("Value of fd: “ + fd);
      System.out.println("Value of fe: “ + fe);
   }
}


The results will be obtained from the above program as follows:

Arithmetic operation for integer type
Value of a: 3
Value of b: 2
Value of c: 6
Value of d: 2
Value of e: -3

Arithmetic operation on floating-point type
Value of fa: 3.0
Value of fb: 2.0
Value of fc: 6.0
Value of fd: 2.0
Value of fe: -3.0


Modulus (Remainder of Division) Operator


In Java, the modulus operator (%) is used to determine the side of an operating division of integer or real number. Consider the following code example:

int a = 11, b = 4;
int c = a % b;

Here, variable c will be worth 3. Where the value of 3 come from? Initially 11 divided by 4 will yield a value of 2. Furthermore, the value of 2 is multiplied by 4 to produce value 8. And the value 3 is derived from 11 minus 8. In other words, 3 is the remainder of a division operation for 11/8.

Now, consider again the following code:

double da = 13.75;
int b = 4
double dc = da % b;

In this case, the variable dc will be worth 1.75, that is remainder of the 13.75/4.

Here is an example of using the modulus operator in the program.

class ModulusDemo {

   public static void main(String[] args) {

      int a = 11, b = 4;
      int c = a * b;

      double da = 13.75;
      double dc = da * b;

      System.out.println("Remainder of division of " + a + "/" + b + " is " + c);
      System.out.println("Remainder of division of " + da + "/" + b + " is " + dc);
   }
}


When executed, the above program will give the following results:

Remainder of division of 11/4 is 3
Remainder of division of 13.75/4 is 1.75


Increment and Decrement Operator


Java provides increment (++) and decrement (--) operator. Increment used to raise the value of the operand with a value of 1, while the decrement is the opposite, to reduce the value of the operand with a value of 1. Actually, these operators is a form of shorthand writing any code. Let your attention to the following code:

a = a + 1;

can be written with the increment operator, as written in the following code:

a++;

Now, for the decrement:

a = a - 1;

can be written with a decrement operator, as written in the following code

a--;

2 comments:

  1. Thank you for sharing valuable information with us.

    Java web application is a type of free programming language used to develop applications and software that are becoming highly in demand on the internet. The software engineer can provide the web application or software in the form of windows, Linux, and MAC OS. There are various types of systems that can not work without java and it is getting necessary day by day even the world’s no.1 application depends on java. Java has become a most popular feature in android smartphone applications that's why Aptech learning provides java training in Dwarka, Janakpuri, and in Gurgaon also. Java training helps to get you complete knowledge about web frameworks, Java testing tools, and design patterns in java.

    Let's get learn about the PHP training course
    PHP is a type of programming language that is most commonly used in dynamic functions such as modifying, collecting, and deleting all types of databases It also helps to encrypt all types of data in the form of HTML and XML file. Aptech learning provides you with a complete PHP training course at a reasonable cost that will help you to grow more in the era of the software industry.

    For more details please visit our website : Aptech Janakpuri

    ReplyDelete