Google+

## Blog Pages

### 49. Arithmetic Operators Operators in Java are just symbols used to perform operations on the provided data. Lets say we have an operator + , which is used to add values or values stored in variables.

Example -

1+2  //Here plus symbol is an operator which can be used to perform addition of two numbers.

Like this we have many operators which perform various operations. Lets learn them practically by following the current and few next posts.

 Addition operator - Program to demonstrate the Addition operator class Addition {   public static void main(String args[])   {     int a=1+9;    // '+' is the operator used for addition i.e. '+' symbol adds 1 and 9 numbers     System.out.println("a="+a);   } } Output of this program: a=10 Adding two decimal numbers and storing in an int type variable class AddingDecimalsStoringInt {   public static void main(String args[])   {      int a=5.5+3.2;   //Adding two decimal numbers i.e. 5.5 and 3.2 and storing the result in the int type variable 'a'      System.out.println("a="+a);   } } Output of this program: Error "Required int values but found decimal values"  i.e. We cant assign decimal values to the int type variables. If we replace the statement int a=5.5+3.2; to int a=(int)5.5+(int)3.2; i.e. by adding type cast to the decimal values, the program will not throw the error, instead it will output a=8 i.e. digits after the decimal points will be ignored while adding. Adding two int numbers and storing in an double type variable class AddingIntegersStoringDouble {   public static void main(String args[])   {      double a=5+3;     //Adding two integers i.e. 5 and 3 and storing the result in the double type variable 'a'      System.out.println("a="+a);   } } Output of this program: a=8.0 Adding two decimal numbers and storing in a double type variable class AddingDecimalNumbersStoringDouble {   public static void main(String args[])   {      double a=5.5+3.2;    //Adding two decimal numbers i.e. 5.5 and 3.2 and storing the result in the double type variable 'a'      System.out.println("a="+a);   } } Output of this program: a=8.7 Subtraction operator - Program to demonstrate the Subtraction operator class Subtraction {   public static void main(String args[])   {      int a=9-1;    // '-' is the operator used for subtraction i.e. '-' symbol deducts 1 from 9.      System.out.println("a="+a);   } } Output of this program: a=8 Subtracting two decimal numbers and storing in an int type variable class SubtractingDecimalsStoringInt {   public static void main(String args[])   {      int a=5.5-3.2;    //Subtracting two decimal numbers i.e. deducting 3.2 from 5.5 and storing the result in the int type variable 'a'      System.out.println("a="+a);   } } Output of this program: Error "Required int values but found decimal values"  i.e. We cant assign decimal values to the int type variables. If we replace the statement int a=5.5-3.2; to int a=(int)5.5-(int)3.2; i.e. by adding type cast to the decimal values, the program will not throw the error, instead it will output a=2 i.e. digits after the decimal points will be ignored while subtracting. Subtracting two int numbers and storing in an double type variable class SubtractingIntegersStoringDouble {   public static void main(String args[])   {      double a=5-3;     //Subtracting two integers i.e.Deducting 3 from 5 and storing the result in the double type variable 'a'      System.out.println("a="+a);   } } Output of this program: a=2.0 Subtracting two decimal numbers and storing in a double type variable class SubtractingDecimalNumbersStoringDouble {   public static void main(String args[])   {      double a=5.5-3.2;    //Subtracting two decimal numbers i.e. deducting 3.2 from 5.5 and storing the result in the double type variable 'a'      System.out.println("a="+a);   } } Output of this program: a=2.3 Multiplication operator - Program to demonstrate the Multiplication operator class Multiplication {   public static void main(String args[])   {      int a=9*1;    // '*' is the operator used for Multiplication i.e. '*' symbol multiplies 9 with 1.      System.out.println("a="+a);   } } Output of this program: a=9 Multiplying two decimal numbers and storing in an int type variable class MultiplyingDecimalsStoringInt {   public static void main(String args[])   {      int a=5.5*3.2;     //Multiplying two decimal numbers i.e. 5.5 from 3.2 and storing the result in the int type variable 'a'      System.out.println("a="+a);   } } Output of this program: Error "Required int values but found decimal values"  i.e. We cant assign decimal values to the int type variables. If we replace the statement int a=5.5*3.2; to int a=(int)5.5*(int)3.2; i.e. by adding type cast to the decimal values, the program will not throw the error, instead it will output a=15 i.e. digits after the decimal points will be ignored while multiplying. Multiplying two int numbers and storing in an double type variable class MultiplyingIntegersStoringDouble {   public static void main(String args[])   {      double a=5*3;     //Multiplying two integers i.e. 5 and 3 and storing the result in the double type variable 'a'      System.out.println("a="+a);   } } Output of this program: a=15.0 Multiplying two decimal numbers and storing in a double type variable class MultiplyingDecimalNumbersStoringDouble {   public static void main(String args[])   {      double a=5.5*3.2;    //Multiplying two decimal numbers i.e. 5.5 from 3.2 and storing the result in the double type variable 'a'      System.out.println("a="+a);   } } Output of this program: a=17.6 Division operator - Programs to demonstrate the Division operator class Division {   public static void main(String args[])   {      int a=10/5;   // '/' is the operator used for Division i.e. '/' symbol divides 10 by 5.      System.out.println("a="+a);   } } Output of this program: a=2 Dividing two decimal numbers and storing in an int type variable class DividingDecimalsStoringInt {   public static void main(String args[])   {      int a=5.5/3.2;       //Dividing two decimal numbers i.e. Dividing 5.5 by 3.2 and storing the result in the int type variable 'a'      System.out.println("a="+a);   } } Output of this program: Error "Required int values but found decimal values"  i.e. We cant assign decimal values to the int type variables. If we replace the statement int a=5.5/3.2; to int a=(int)5.5/(int)3.2; i.e. by adding type cast to the decimal values, the program will not throw the error, instead it will output a=1 i.e. digits after the decimal points will be ignored while dividing. Dividing two int numbers and storing in an double type variable class DividingIntegersStoringDouble {   public static void main(String args[])   {      double a=5/3;     //Dividing two integers i.e. Dividing 5 by 3 and storing the result in the double type variable 'a'      System.out.println("a="+a);   } } Output of this program: a=1.0 Dividing two decimal numbers and storing in a double type variable class DividingDecimalNumbersStoringDouble {   public static void main(String args[])   {      double a=5.0/3;     //Dividing two decimal numbers i.e. Dividing 5.0 by 3 and storing the result in the double type variable 'a'      System.out.println("a="+a);   } } Output of this program: a=1.6666666666666667 If you clearly observed the different between the last two programs, they are almost same but the statement double a=5/3; in program one is written as the statement double a=5.0/3;  i.e. in the first statement double a=5/3; though the result of the integers gets stored in double type variable the output value is 1.0 instead of 1.6666666666666667 because in the 5/3 operation, both 5 and 3 are integers hence the result will be calculated by ignoring the digits after the decimal point value i.e. 1 is the result of 5/3 operation but when assigned to double a; variable , it gets converted to 1.0. Where as in the statement double a=5.0/3; one of the value is a decimal in 5.0/3 operation hence the result will be in decimal format only i.e. 1.6666666666666667 and later it gets assigned to variable a which of double type as 1.66666666666666667. Modulus operator The Modulus operator '%' returns the reminder of a division operator. Program to demonstrate the operation of modulus operator on integers class ModulusIntegers {   public static void main(String args[])   {      int a=8%3;    //Finding the reminder of 8 when divided by 3 using the modulus operator '%'      System.out.println("a="+a);   } } Output of this program: a=2 Program to demonstrate the operation of modulus operator on decimals class ModulusDecimals {   public static void main(String args[])   {      double a=8.568%3;   //Finding the reminder of 8.568 when divided by 3 using the modulus operator '%'      System.out.println("a="+a);   } } Output of this program: a=2.567999999999999 Compound Assignment Operators Java provides special operators called Compound Assignment operators which combine an arithmetic operation with an assignment '=' Program to demonstrate the '+=' compound assignment operator class PlusEqualCompoundAssignmentOperator {    public static void main(String args[])    {       int a=4;       a+=4;   //Using Compound assignment operator to increment the variable 'a' value by 4 i.e. 4+4       System.out.println("a="+a);    } } Output of this program: a=8 The a+=4; statement works like a=a+4 i.e. a=4+4; i.e. a=8 Program to demonstrate the '-=' compound assignment operator class MinusEqualCompoundAssignmentOperator {    public static void main(String args[])    {       int a=4;       a-=4;    //Using Compound assignment operator to decrement the variable 'a' value by 4 i.e. 4-4       System.out.println("a="+a);    } } Output of this program: a=0 The a-=4; statement works like a=a-4 i.e. a=4-4; i.e. a=0 Program to demonstrate the '*=' compound assignment operator class MultiplyEqualCompoundAssignmentOperator {    public static void main(String args[])    {       int a=4;       a*=4;   //Using Compound assignment operator to multiply the variable 'a' value by 4 i.e. 4*4       System.out.println("a="+a);    } } Output of this program: a=16 The a*=4; statement works like a=a*4 i.e. a=4*4; i.e. a=16 Program to demonstrate the '%=' compound assignment operator class PercentileEqualCompoundAssignmentOperator {    public static void main(String args[])    {       int a=4;       a%=3;    //Using Compound assignment operator to find the reminder after dividing the variable 'a' value by 3       System.out.println("a="+a);    } } Output of this program: a=1 The a%=3; statement works like a=a%3 i.e. a=4%3; i.e. a=1 Program to demonstrate the increment operator ++VariableName class PrefixIncrementOperator {    public static void main(String args[])    {       int a=4;       ++a;     //Increment the value of variable 'a' by one       System.out.println("a="+a);    } } Output of this program: a=5 Observe the value of variable 'a' got incremented by 1 i.e. a=a+1 i.e. a=4+1 i.e. a=5 Program to demonstrate the increment operator VariableName++ class PostfixIncrementOperator {    public static void main(String args[])    {       int a=4;       a++;      //Increment the value of variable 'a' by one       System.out.println("a="+a);    } } Output of this program: a=5 Observe the value of variable 'a' got incremented by 1 i.e. a=a+1 i.e. a=4+1 i.e. a=5 Program to demonstrate the difference between the increment operators ++VariableName  & VariableName++ So what is the difference between the prefix increment operator and postfix increment operator. To understand it first we have to understand the below program: class PrefixVsPostfix {    public static void main(String args[])    {       int a=1;       int b=1;         System.out.println("The value of variable 'a' before incremental using prefix operator="+a);       System.out.print("The value of variable 'a' while incremental using prefix operator=");       System.out.println(++a);       System.out.print("The value of variable 'a' after incremental using prefix operator=");       System.out.println(a);         System.out.println();         System.out.println("The value of variable 'b' before incremental using postfix operator="+b);       System.out.print("The value of variable 'b' while incremental using postfix operator=");       System.out.println(b++);       System.out.print("The value of variable 'a' after incremental using postfix operator=");       System.out.println(b) ;    }  } Output of this program: The value of variable 'a' before incremental using prefix operator=1 The value of variable 'a' while incremental using prefix operator=2    (Incremented and printed) The value of variable 'a' after incremental using prefix operator=2 The value of variable 'b' before incremental using postfix operator=1 The value of variable 'b' while incremental using postfix operator=1 (Printed without increment) The value of variable 'b' after incremental using postfix operator=2  (Incremented here) So the output clearly explains the difference between the prefix and postfix incremental operators. Another program to demonstrate the difference between the increment operators ++VariableName  & VariableName++ class PrefixVsPostfix {    public static void main(String args[])    {       int a=1;       int b=2;       int c=++b;    // Here the value of variable 'b' will get incremented first and then the incremented value gets assinged to vartiable 'c'       int d=a++;    // Here the value of variable 'a' will get assigned to variable 'd' and later the value of variable 'a' gets incremented         System.out.println("a="+a);       System.out.println("b="+b);       System.out.println("c="+c);       System.out.println("d="+d);      }  } a=2 b=3 c=3 d=1 Program to demonstrate the decrement operator --VariableName class PrefixDecrementOperator {    public static void main(String args[])    {       int a=4;       --a;     //decrements the value of variable 'a' by one       System.out.println("a="+a);    } } Output of this program: a=3 Observe the value of variable 'a' got decremented by 1 i.e. a=a-1 i.e. a=4-1 i.e. a=3 Program to demonstrate the decrement operator VariableName-- class PostfixDecrementOperator {    public static void main(String args[])    {       int a=4;       a--;     //decrements the value of variable 'a' by one       System.out.println("a="+a);    } } Output of this program: a=3 Observe the value of variable 'a' got decremented by 1 i.e. a=a-1 i.e. a=4-1 i.e. a=3 Program to demonstrate the difference between the decrement operators --VariableName  & VariableName-- So what is the difference between the prefix decrement operator and postfix decrement operator. To understand it first we have to understand the below program: class PrefixVsPostfix {    public static void main(String args[])    {       int a=1;       int b=1;         System.out.println("The value of variable 'a' before decrement using prefix operator="+a);       System.out.print("The value of variable 'a' while decrement using prefix operator=");       System.out.println(--a);       System.out.print("The value of variable 'a' after decrement using prefix operator=");       System.out.println(a);         System.out.println();         System.out.println("The value of variable 'b' before decrement using postfix operator="+b);       System.out.print("The value of variable 'b' while decrement using postfix operator=");       System.out.println(b--);       System.out.print("The value of variable 'a' after decrement using postfix operator=");       System.out.println(b) ;    }  } Output of this program: The value of variable 'a' before decrement using prefix operator=1 The value of variable 'a' while incremental using prefix operator=0  (decremented and printed) The value of variable 'a' after incremental using prefix operator=0 The value of variable 'b' before incremental using postfix operator=1 The value of variable 'b' while incremental using postfix operator=1 (Printed without decremented) The value of variable 'b' after incremental using postfix operator=0 (decremented here) So the output clearly explains the difference between the prefix and postfix decrement operators. Another program to demonstrate the difference between the decrement operators --VariableName  & VariableName-- class PrefixVsPostfix {    public static void main(String args[])    {       int a=1;       int b=2;       int c=--b;    // Here the value of variable 'b' will get decremented first and then the decremented value gets assigned to variable 'c'       int d=a--;    // Here the value of variable 'a' will get assigned to variable 'd' and later the value of variable 'a' gets decremented         System.out.println("a="+a);       System.out.println("b="+b);       System.out.println("c="+c);       System.out.println("d="+d);      }  } a=0 b=1 c=1 d=1

Please comment below to feedback or ask questions.

How to use the Relational operators will be explained in the next post

#### 5 comments:

Chandana said...

Hi Arun,
in the second program can't we use like this..may i know the reason why it is not possible

int a = 10.05+ 0.01
double b = (double) a

B Dinesh said...

in last example, result shouldn't be like below
1
1
1
1

Arun Motoori said...

@B Dinesh - You are wrong.

Arun Motoori said...

@Chandana - You will get compiler error as you cant assign a decimal value to an int data type declared variable.

vinaya m said...

Hi Arun,

Can you explain the difference between the following two programs? I am bit confused between the outputs of both programs.

(1)
class PrefixVsPostfix
{
public static void main(String args[])
{
int a=1;
int b=2;
int c=++b; // Here the value of variable 'b' will get incremented first and then the incremented value gets assinged to vartiable 'c'
int d=a++; // Here the value of variable 'a' will get assigned to variable 'd' and later the value of variable 'a' gets incremented

System.out.println("a="+a);
System.out.println("b="+b);
System.out.println("c="+c);
System.out.println("d="+d);

}
}

a=2
b=3
c=3
d=1

(2)
class PrefixVsPostfix
{
public static void main(String args[])
{
int a=1;
int b=2;

System.out.println("a="+a);
System.out.println("b="+b);

int c=++b;
int d=a++;

System.out.println("c="+c);
System.out.println("d="+d);

}
}

a=1
b=2
c=3
d=2