arithmetic operator

Arithmetic, Assignment and Unary Operator in Java

arithmetic operatorIn this chapter you will learn:

  1. What is assignment operator and their symbol?
  2. What is arithmetic operator and their symbol?
  3. What is unary operator and their symbol?
  4. Programming example

What is assignment operator in Java?

Assignment(=) operator is most common operator in Java. It is used for assigning value to variable. For example
int num = 5;

What is arithmetic operator in java?

Arithmetic operator is an operator which provides addition, subtraction, multiplication, division and reminder. It performs mathematical operation on operands.

Operator Description Example
+ It is called as Plus operator which adds integral value. num1 + num2
It is called Minus operator which subtracts integral value. num1 – num2
* It is called Multiply operator which multiply integral value. num1 * num2
/ It is called Divide operator which divide integral value. num1 / num2
% It is called Reminder operator which tells reminder of two integral values. num1 % num2
Programming Example:
class ArithmeticOperators
{
  public static void main(String[] args)
  {
    int num1, num2, result;
    num1=10;
    num2=20;
    
    //Addition
    result=num1+num2;
    System.out.println("Addition : "+result);
    
    //Subtraction
    result=num2-num1;
    System.out.println("Subtraction : "+result);
    
    //Multiplication
    result=num1*num2;
    System.out.println("Multiplication : "+result);
    
    //Division
    result=num2/num1;
    System.out.println("Division : "+result);
    
    //Reminder
    result=num1/4;
    System.out.println("Reminder : "+result);    
  }
}

Output

D:\JavaProgram>javac ArithmeticOperators.java

D:\JavaProgram>java ArithmeticOperators
Addition : 30
Subtraction : 10
Multiplication : 200
Division : 2
Reminder : 2

D:\JavaProgram> __

What is unary operator?

Unary operator is such kind of operator which requires only one operand to perform task. This type of operator is used for increasing or decreasing value, negating the expression or inverting the Boolean value.

Operator Description Example
+ Unary Plus Operator: It indicates positive value. (However numbers are positive by default) 35 or +35
Unary Minus Operator: It indicates negative value -35
++ Increment Operator: It increment value by 1 num1++
Decrement Operator: It decrements value by 1 num1–
! Logical Complement Operator: (also known as Not Operator) – It inverts the Boolean expression. (true to false or false to true) if(!(true))

Programming Example

class UnaryOperator
{
  public static void main(String[] args)
  {
    int number=10;
    
    number++;
    System.out.println(number);
    //number is now 11
    
    number=10;
    number--;
    System.out.println(number);
    //number is now 9
    
    number=10;
    number = -number;
    System.out.println(number);
    //number is now -10
    
    boolean bn=true;
    System.out.println(!bn);
      //bn is now false
  }
}

Output

D:\JavaProgram>Javac UnaryOperator.java

D:\JavaProgram>java UnaryOperator
11
9
-10
false

D:\JavaProgram> __

Summary

In this chapter you have learned about assignment, arithmetic and unary operator in java. The next chapter you will learn about Relational Operator in Java.

Leave a Reply

Your email address will not be published.