Operators in Java - Types and Examples


In this article, I will explain about operators in Java, classification of operators, Types of operator like Arithmetic operators, Increment operator, Decrement operator, Assignment operator, etc.

Operators in Java

Operators are used to perform operations on variables.
Classification of operators
  • Unary operators: Operator which performs operation on single variable.
    Unary minus operator (-)
    It is used to negate the provided value.
    Example:
    Int a=2;
    System.out.println(-a);
    Output: -2

  • Binary operators: Operator which performs operation on two variables.

  • Ternary operators: Operator which performs operation on three variables.

Types of operators:

  • Arithmetic operators:
    There are five arithmetic operators in java.
    Suppose we have two variables a and b. And a=5 and b=2
    1. Addition operator (+):
      Syntax: a+b
      Example: System.out.println(a+b);
      Output: 7

    2. Subtraction operator (-):
      Syntax: a-b
      Example: System.out.println(a-b);
      Output: 3

    3. Multiplication operator (*):
      Syntax: a*b
      Example: System.out.println(a*b);
      Output: 10

    4. Division operator (/):
      Syntax: a/b
      Example: System.out.println(a/b);
      Output: 2

    5. Modulus operator (%): This will give remainder of division
      Syntax: a%b
      Example: System.out.println(a%b);
      Output: 1

  • Increment operator:
    This operator is used to increase the value of variable by 1.
    There are two types of increment operator.
    1. Pre incrementation operator
      In this operator, ++ is written before variable.
      So value of variable is incremented first before next operation.
      Example:
      int x=2;
      int y=3;
      x=++y;
      System.out.println("Value of x is: " +x);
      System.out.println("Value of y is: " +y);

      Output:
      Value of x is: 4
      Value of y is: 4

    2. post incrementation
      In this operator, ++ is written after variable.
      So value of variable is incremented after all other operations.
      Example:
      int x=2;
      int y=3;
      x=y++;
      System.out.println("Value of x is: " +x);
      System.out.println("Value of y is: " +y);

      Output:
      Value of x is: 3
      Value of y is: 4

  • Decrement operator
    This operator is used to decrease the value of variable by 1.
    There are two types of decrement operator.
    1. Pre decrementation operator
      In this operator, -- is written before variable.
      So value of variable is decremented first before next operation.
      Example:
      int x=2;
      int y=3;
      x=--y;
      System.out.println("Value of x is: " +x);
      System.out.println("Value of y is: " +y);

      Output:
      Value of x is: 2
      Value of y is: 2

    2. Post decrementation
      In this operator, -- is written after variable.
      So value of variable is decrementated after all other operations.
      Example:
      int x=2;
      int y=3;
      x=y--;
      System.out.println("Value of x is: " +x);
      System.out.println("Value of y is: " +y);

      Output:
      Value of x is: 3
      Value of y is: 2

  • Assignment operator
    This operator is used to store value into variable.
    Example: Any value or variable or result of any expression.
    int x=2;
    Here, we are assigning value 2 to x variable by using assignment operator.

  • Relational operator
    It is used for the comparing two variables.
    Types of relational operator
    Let's take one example to understand the relational operators.
    int x=2;
    int y=3;
    if (x>y)         //Greater than operator (>): It will verify if x is greater than y
    {
    System.out.println("x is greater than y");
    }
    if(x>=y)         // Greater than or equal to (>=): It will verify if x is greater than or equal to y
    {
    System.out.println("x is greater than or equal to y");
    }
    if(x{
    System.out.println("x is less than y");
    }
    if(x<=y)        // Less than or equal to (<=): It will verify if x is less than or equal to y
    {
    System.out.println("x is less than or equal to y");
    }
    if(x==y)         // Equal to operator (==): It will verify if x is equal to y
    {
    System.out.println("x is equal to y");
    }
    if(x!=y)         // Not equal to operator (!=): It will verify if x is not equal to y
    {
    System.out.println("x is not equal to y");
    }


    Output:
    x is less than y
    x is less than or equal to y
    x is not equal to y

  • Logical operator
    It is used in compound conditions. Compound conditions means combination of simple conditions.
    Three types of logical operators
    1. And operator (&&)
      And operator is used to when we want to verify that all the conditions are true.
      Example:
      int x=2;
      int y=3;
      if((x == 2) && (y == 3)){
      System.out.println("If block executed");
      }else{
      System.out.println("Else block executed");
      }

      Output: If block executed
      Here, our both conditions(x=2 and y=3) are true that's why printing 'If block executed' statement.

    2. OR operator (||)
      OR operator is used to when we want to verify that at least one condition is true.
      Example:
      int x=2;
      int y=3;
      if((x == 4) || (y == 3)){
      System.out.println("If block executed");
      }else{
      System.out.println("Else block executed");
      }

      Output: If block executed
      Here, one condition(x=4) is false and one condition(y=3)is true so it will execute if block.

    3. Not operator (!)
      Not operator is used to check the opposite of specified condition.
      int x=2;
      int y=3;
      if(!(x == 4)){
      System.out.println("If block executed");
      }else{
      System.out.println("Else block executed");
      }

      Output: If block executed
      Here, we have provided x=4 condition and used NOT operator, so it will check if x is not equal to 4. So it is satisfying if block condition and hence printing 'If block executed'.

  • Boolean operator
    Boolean operators are used for Boolean variables and produce Boolean result.
    There are four types of Boolean operator
    1. Boolean and operator (&)

    2. Boolean or operator (|)

    3. Boolean not operator (!)

    4. Boolean XOR operator(^)

    It will work as in below table.
    ABA&BA|B!AA^B
    trueTruetruetruefalsefalse
    trueFalsefalsetruefalsetrue
    falseTrueFalsetrueTruetrue
    falsefalsefalsefalsetruefalse

    boolean A = true;
    boolean B = false;

    System.out.println("A OR B = "+(A|B));
    System.out.println("A AND B = "+(A&B));
    System.out.println("NOT A = "+(!A));
    System.out.println("A XOR B = "+(A^B));

    Output:
    A OR B = true
    A AND B = false
    NOT A = false
    A XOR B = true

  • Member operator
    It is also called as dot operator specified as '.'
    It tells about member of a package or class.
    Package has many classes, so to refer class of a package we can write
    Packagename.classname;
    Also to refer any method of class or variable of class dot operator is used.
    Classname.variablename;
    Classname.methodname;

    Example:
    System.out.println();
    Here, System is built-in class in java.lang package and out is static member of System class. Also out is an instance of java.io.PrintStream. And println is method of java.io.PrintStream. So to represent the relation we have used dot(.) operator.

  • instanceof operator
    Instanceof operator is used to check whether object belongs to class or not.
    Syntax:
    Boolean variable = ObjectName instanceof ClassName
    Example:
    Boolean y = obj instanceof Customer;
    So, if obj is object of Customer class then true will be returned to y otherwise false.

  • new operator
    New operator is used at the time of object creation of classes.
    Syntax:
    ClassName object = new ClassName()
    Example:
    For customer class if we want to create object 'obj' then below is the code
    Customer obj = new Customer()

  • cast operator
    It is used to convert one data type into another data type. Data type in which you want to convert the variable is written inside single braces.
    Example:
    double f = 34.21;
    int i = (int)f;
    System.out.println("Value of i:" + i);

    Output: Value of i:34
    In this example, (int) is called as cast operator.

  • Ternary operator
    Syntax: X = (condition) ? [true statement] : [False statement]
    Example: X = (aHere, it will verify provided condition i.e. a is less than b. If it is true then it will execute true statement block and if it is false then it will execute false statement block.


Related Articles

Creating new JEE 5 applications (Java and EJB Web Services) using RAD 7.5

Now days, most of the enterprise applications are developed in RAD 7.0+ (Rational Application Developer). The Web service application can be written in Java or in EJB. For creating Web services in Java or EJB, the vast documentation is available. Referring such documentation during speedy development period becomes a hectic job. So to avoid it, this article will help to easily develop a JEE5 application using simple steps.

Program for Addition performer in Java

This article provides a description to the addition performer in Java, that can perform addition operation on two input numbers. Code for the addition performer is provided in the article. The program is based on use of Swings. In Java Frames can be created and different text fields alongwith labels corresponding to them can be added. And use of all these elements can develope an efficient adder.

What is difference between an Interface and Abstract class in Java?

Are you looking for an article which explains the concepts of an Interface and Abstract class in simple language? Then your search ends here. Read this article, herein the detail about abstract class and an Interface is given. Also the important differences between an Interface and Abstract class is in Java is explained. This article highlights the important aspects regarding whether abstract class to be used or an Interface is to be used for given requirement of Enterprise application.

Directory Structure - XML Output

In this article, I have submitted a Java program. We will be able to get the dynamic XML file with treeview represents a folder and file structure of a given folder path using it. This Java program also show how to use a recursive function to read a directory structure and then save it to XML file. I commented properly for your better understanding how the program flow goes. The output XML file is well-formed

More articles: Java

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: