Skip to main content

Java Operators

Operators is an symbols which are play important role in programming. That can be used to perform mathematical operation, compression and assignment operation. That is a simple way to describe the role of an operator. But that is a basic level, operator is much powerful in programming. That are used basic or special purpose.

Generally operator are involved of many place in our code, Such as when, define a similar type of multiple variable then there are use comma operator to separating the variable. Dot operator are used to method of class and there are in similar way many operator are used to perform some specific task.

In this post are given examples which are commonly used to mathematical and logical operation.

Arithmetic Operator

Arithmetic Operator are work in two operands, that is also know as binary operators. We can thinking about this type of operators. This operator are works two operands such as left and right. And operator are exist in between of them. operator is decide to which operation are performed to those operand. In given table mention of all arithmetic operators.

Operator Name
+ Addition(+)
- Subtract (-)
* multiplication (*)
/ Divide
% remainder operator

Basically arithmetic operator are used to perform 6 type of operation. compiler are know about what is the role of symbol that are applied to operands (variable value or constant literals).

//Arithmetic Operator Example
class ArithmeticOperator{
    public static void main(String[] args) {
        int a=10,b=3;
        int result=a+b;//13
        System.out.println(a+" + "+b+"  = "+(result));
        
        result=a-b;//7
        System.out.println(a+" - "+b+"  = "+(result));
        
        result=a*b;//30
        System.out.println(a+" * "+b+"  = "+(result));
        
        result=a/b;//3
        System.out.println(a+" / "+b+"  = "+(result));
        
        result=a%b;//1
        System.out.println(a+" % "+b+"  = "+(result));

    }
}

Output

10 + 3  = 13
10 - 3  = 7
10 * 3  = 30
10 / 3  = 3
10 % 3  = 1

In java Programming, (+) addition operator are used to adding the numeric value. (-) operator are used to Subtract two values, (*) operator are used to multiplication of two values. and similarly (/) operator are divide the two values and (%) are used to get remainder of two values. Modulus % operator can works on a any number type (integer,float,double).

Logical Operator

Logical operator are work in boolean values. && operator are used to check expression condition when both left and right the expression are produced boolean true value. Then that are produce an true boolean result. Otherwise produce false. In given list are mentioned logical operators.

Operator Name
&& Logical AND operator.
|| Logical OR Operator
! Logical NOT Operator

Example of logical AND operator.

//Logical && (and) Operator Example
class LogicalOperator{
    public static void main(String[] args) {
        
        boolean result=true&&true;
        System.out.println(result); //true

        result=false&&false;
        System.out.println(result); //false

        result=true&&false;
        System.out.println(result);//false

        result=false&&true;
        System.out.println(result); //false
        
    }
}

Output

true
false
false
false

Logical Or (||) is an binary operator, That is work on boolean values. This operator are produce true value when any one expression are produced true result (left and right operand result).

//Logical Or Operator Example
class LogicalOperator{
    public static void main(String[] args) {
        
        boolean result=true||true;
        System.out.println(result); //true

        result=false||false;
        System.out.println(result); //false

        result=true||false;
        System.out.println(result);//true

        result=false||true;
        System.out.println(result); //true
        
    }
}

Output

true
false
true
true

Logical Not (!) is an unary operator that are work on boolean value. when expression is produce true value that is modified this result to false. and when expression is produce false boolean value then that is change this result to true boolean value.

//Logical ! (not) Operator Example
class LogicalOperator{
    public static void main(String[] args) {
        boolean trueStatus = true;
        boolean falseStatus = !trueStatus; //use logical not
        System.out.println(trueStatus); //true
        System.out.println(falseStatus); //false
    }
}

Output

true
false

We can also use this operator to check logical condition result. The main role of Logical Not operator is inverts the boolean result. And this is prefix operator.

//Logical ! (not) Operator Example
class LogicalOperator{
    public static void main(String[] args) {
        if(!(5 > 15)){
            // (5>15) = false =>  !false = true
            System.out.println("Five is not greater than 15");
        }
    }
}

Output

Five is not greater than 15

Bitwise Operators

Bitwise operator is based on binary value and that are work on two operands. Normally all operators are produce boolean values but that is work on a binary bit level modification. In given table mentioned all Bitwise operator.

Operator Name
& Binary AND operator.
| Binary OR Operator
^ Binary XOR Operator
~ Binary Ones Complement Operator
<< Binary Left Shift Operator
>> Binary Right Shift Operator

Binary AND operator

//Bitwise Operators
class BitwiseOperators{
    public static void main(String[] args) {

        //Example Binary And Operator


        /*
        *  Binary of 5 =   0  1  0   1
        *  Binary of 4 =   0  1  0   0
        *            &
        *--------------------------------
        *     Result 4 =   0  1  0   0
        */

        int a = 5 & 4;
        System.out.println(a); //4
        
    }
}

Output

4

Binary Or operator

//Bitwise Operators
class BitwiseOperators{
    public static void main(String[] args) {

        //Example Binary OR Operator

        /*
        *  Binary of 3 =   0  0  1   1
        *  Binary of 4 =   0  1  0   0
        *            |
        *--------------------------------
        *     Result 7 =   0  1  1   1
        */

        int a = 3 | 4;
        System.out.println(a); //7
        
    }
}

Output

7

Binary XOR operator

//Bitwise Operators
class BitwiseOperators{
    public static void main(String[] args) {

        //Example Binary XOR Operator

        /* ( both bit are different then true)
        *  Binary of 5 =   0  1  0   1
        *  Binary of 4 =   0  1  0   0
                     ^   
        *--------------------------------
        *     Result 1 =   0  0  0   1

            When  0  1  = 1
            When  0  0  = 0
            When  1  1  = 0
        */
        int a = 5 ^ 4;
        System.out.println(a); //1
        
    }
}

Output

1

Binary Ones Complement Operator

//Bitwise Operators (Binary Ones Complement Operator)
class BitwiseOperators{
    public static void main(String[] args) {
        
        int testOne=15,testTwo=-6;

        /*Bitwise 1s Complement Operator
        Example 1:

          15  = 1  1  1  1 (binary of 15)
          add            1 (binary of 1)
        +   
        --------------------------------
        Result= 1 0  0  0  0 (binary of 16)
        --------------------------------- 
        output= - Result
        output= - 16

        Example 2: first remove that (- sign)
          6   = 0  1  1  0 (binary of  6)
          minus          1 (binary of  1)
        -                  (Perform minus)
        --------------------------------
        Result =  0  1  0  1 (binary of 5)
        Result= 5 
        //add remove sign
        Result =-5 
        --------------------------------- 
        //multiply by (- sign)
        output= - Result
        output= - (-5)
        output= 5

        */
        int result = ~testOne;
        System.out.println(result); //-16
        result = ~testTwo;
        System.out.println(result); //5
    }
}

Output

-16
5

Binary Left Shift Operator

//Bitwise Operators (Binary Left Shift Operator)
class BitwiseOperators{
    public static void main(String[] args) {
        
        /*Bitwise <<
        Example 1:

          15          = 0  0  1  1  1  1 (binary of 15)
        left shift 2  = 1  1  1  1  0  0  
          
        --------------------------------
        60     =     1  1  1  1  0  0 
        ---------------------------------
        Or
        Formula   = data * (2^ left shift)
        example 1 =  15   *(2^2)  2 bit shift
                =  15*4
                =  60
        example 2 =  2 *(2^5) 
            =  2 *(32)
            =  64          
        */
        
        System.out.println(15 << 2); //60
    
        System.out.println(2 << 5); //64
    }
}

Output

60
64

Binary Right Shift Operator

//Bitwise Operators (Binary right Shift Operator)
class BitwiseOperators{
    public static void main(String[] args) {
        
        /*Bitwise >>
        Example 1:

          15          = 1  1  1  1 (binary of 15)
        right shift 2 = 0  0  1  1  
          
        --------------------------------
        3     =        0  0  1   1
        ---------------------------------     
        */
        
        System.out.println(15 >> 2); //3
    }
}

Output

3

Relational Operators

Relational operator are work on two operands, and normally that are used to compare operands value. This operator are produce boolean result. And which are normally used in conditional statement to check expression.

Operator Name
> greater than
< less than
== equal to equal to
>= greater than equal to
<= less than equal to
!= Not equal to

Assignment Operators

Assignment operator are used to assign a value of right hand side to left hand variables. In given table are view short form of assignment operator. which are perform some operation of two operand and result are store in the left side variable.

Operator Meaning
= equal
*= Multiply and assign
+= Add and assign
/= Divide and assign
-= Subtract and assign
%= calculate remainder and assign

Basically normal (=) assignment operator are used to set value of variable. This are assign value according to data type of variable. But short form we are combine this operator and arithmetic operator and perform two type of operation. First are performed arithmetic operation and resultant are store by assignment operator.

Ternary Conditional Operator

This operators is similar to C Ternary operator. There is divided into three section, first one are expression. If result of first expression result is true then work on second section expression. And if first expression is false then it will executing on third section expression.

//section 1             section 2    section 3
expression/condition ?     Pass   :   Fail 

For example

//Ternary Operators Example
class TernaryOperators{
    public static void main(String[] args) {
        
        Boolean errorStatus=false;
        //expression
        String result=(errorStatus==true) ? "Sorry Error is Found" : "Awesome Error Free code";
        System.out.println(result);
    }
}

Output

Awesome Error Free code

This is actually shortcut form of if-else statement. And normally that are used to assign value of variables.

new operator in Java

new is an operator of java program. The role of this operator is they are create dynamic memory to class object and returning the reference of this created memory. And class variable are use this allocated memory of by this reference during the program execution. The role is simple but powerful. Normally new operator are execute class constructor and set initial value to class instance variables.

//Example of new operator
public class MyBook{
    //Data field of class
    private String name,genre,language;
    private double price;
    private int edition;

    //Set inital values to class variables
    public MyBook(String name,
                  String genre,
                  String language,
                  double price,
                  int edition){

        this.name=name;
        this.genre=genre;
        this.language=language;
        this.price=price;
        this.edition=edition;

    }
    //Display book details
    public void bookDetails(){
        System.out.println("\nName : "+name);
        System.out.println("Genre : "+genre);
        System.out.println("Language : "+language);
        System.out.println("Price : "+price);
        System.out.println("Edition : "+edition);

    }
    
    public static void main(String[] args){
        //New operator create an instance
        MyBook  java = new MyBook("Java Programming",
                             "Programming",
                             "English",
                             100000.5,
                             2);
        MyBook  novel = new MyBook("Root",
                             "Literary Criticism",
                             "English",
                             522.62,
                             5);

        java.bookDetails();
        novel.bookDetails();
    }
}
Create instance using new operator

Output

Name : Java Programming
Genre : Programming
Language : English
Price : 100000.5
Edition : 2

Name : Root
Genre : Literary Criticism
Language : English
Price : 522.62
Edition : 5

Array in java programming always create dynamically. here we can use primitive data type to define element type of array. wrapper class are provide an availability to implement variable using new operator.

//Example of new operator
public class OperatorExample{

    public static void main(String[] args){
        byte byteData= 2;
        Byte byteObj = new Byte(byteData);

        byte shortData= 10;
        Short shortObj = new Short(shortData);

        Character charObj = new Character('A');
        Integer intObj = new Integer(5);
        Long longObj = new Long(5000000);
        Float floatObj = new Float(6.8f);
        Double doubleObj = new Double(5.81);
        

        System.out.println("Byte object : " + byteObj);
        System.out.println("Short object : " + shortObj);
        System.out.println("Character object : " + charObj);
        System.out.println("Integer object : " + intObj);
        System.out.println("Long object : " + longObj);
        System.out.println("Float object : " + floatObj);
        System.out.println("Double object : " + doubleObj);
    }
}

Output

Byte object : 2
Short object : 10
Character object : A
Integer object : 5
Long object : 5000000
Float object : 6.8
Double object : 5.81




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment