Skip to main content

Introduction to Java Programming

  What is Java? Java is a programming language and a platform. Java is a high-level, robust, object-oriented and secure programming language. Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995. James Gosling is known as the father of Java. Firstly, it was called  Greentalk  and the file extension was  .gt . After that, it was called  Oak . Initially Java was designed for small, embedded systems in electronic appliances like set-top boxes. Platform:  Any hardware or software environment in which a program runs, is known as a platform. Since Java has a runtime environment (JRE) and API(Application Programming Interface), it is called a platform. The principles for creating Java programming were "Simple, Robust, Portable, Platform-independent, Secured, High performance, Multithreaded, Architecture neutral, Object-oriented, Interpreted, and Dynamic". Currently, Java is used in internet programming, mobile devices, games, e-business so

7. Operators in Java

 

Operators

An operator is simply a symbol that is used to perform a specific operation on operands. There are many types of operators in Java as given below:

  • Unary Operators
  • Arithmetic Operators
  • Comparison Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators

Unary Operators

  • The Java unary operators require only one operand.
  • Unary operators are used to perform various operations i.e.
    • incrementing / decrementing a value by one
    • negating an expression
    • inverting the value of a Boolean
class Unary {
    public static void main(String[] arr) {
        int msd = 7, abd = 18;
        boolean cond = true;
        System.out.println("Minus operator: " + (-abd));
        System.out.println("NOT operator: " + !cond);
        System.out.println("Increased but previous value is retained by Post-Increment Operator: " + (msd++)); // msd++ = 7 but msd = 8
        System.out.println("Instantly increase by Pre-Increment Operator: " + (++msd)); // ++8 = 9 and msd = 9
        System.out.println("Decreased but previous value is retained by Post-Decrement Operator: " + (abd--)); // abd-- = 18 but abd = 17
        System.out.println("Instantly decrease by Pre-Decrement Operator: " + (--abd)); // --17 = 16 and abd = 16
        System.out.println("One's Complement by bitwise operator: " + (~abd));
    }
}

Output :

Minus operator: -18
NOT operator: false
Increased but previous value is retained by Post-Increment Operator: 7
Instantly increase by Pre-Increment Operator: 9
Decreased but previous value is retained by Post-Decrement Operator: 18
Instantly decrease by Pre-Decrement Operator: 16
One's Complement by bitwise operator: -17

Note: Post-Increment or Post-Decrement operators first uses the value and then increments or decrements respectively. While Pre-Increment or Pre-Decrement operators first increments or decrements the value, then uses the incremented or decremented value.

Arithmetic Operators

  • Arithmetic operators are used to perform mathematical operations such as addition, multiplication etc. on expressions.
  • Arithmetic operators cannot work with Booleans.
  • Modulus (%) can work on floats and doubles.
class Arithmetic {
    public static void main(String[] arr) {
        double a = 4, A = 4;
        double b;
        b = 6 + a;
        System.out.println("Addition: " + b);
        b = 6 - a;
        System.out.println("Subtraction: " + b);
        b = 6 * a;
        System.out.println("Multiplication: " + b);
        b = 6 / a;
        System.out.println("Division: " + b);
        b = 6 % a;
        System.out.println("Modulus: " + b);
        a++;
        System.out.println("Increment: " + a);
        A--;
        System.out.println("Decrement: " + A);
    }
}

Output :

Addition: 10.0
Subtraction: 2.0
Multiplication: 24.0
Division: 1.5
Modulus: 2.0
Increment: 5.0
Decrement: 3.0

Comparison Operators

  • Comparison operators are used to compare two operands.
  • These operators returns a Boolean value.
class Comparison {
    public static void main(String[] arr) {
        int a = 6;
        System.out.println("Is a equals to 6: " + (a == 6));
        System.out.println("Is a equals to 10: " + (a == 10));
        System.out.println("Is a not equals to 6: " + (a != 6));
        System.out.println("Is a not equals to 10: " + (a != 10));
        System.out.println("Is a greater than 2: " + (a > 2));
        System.out.println("Is a less than 2: " + (a < 2));
        System.out.println("Is a greater than or equals to 6: " + (a >= 6));
        System.out.println("Is a less than or equals to 2: " + (a <= 2));
    }
}

Output :

Is a equals to 6: true
Is a equals to 10: false
Is a not equals to 6: false
Is a not equals to 10: true
Is a greater than 2: true
Is a less than 2: false
Is a greater than or equals to 6: true
Is a less than or equals to 2: false

Logical Operators

  • These operators determine the logic in an expression containing two or more variables.
  • These operators returns a Boolean value and is performed on Boolean expressions.
class Logical {
    public static void main(String[] arr) {
        boolean x = true;
        boolean y = false;
        System.out.println("\tFor Logical AND...");
        System.out.println("false AND false = " + (y && y) + "\nfalse AND true = " + (y && x) + "\ntrue AND false = "
                + (x && y) + "\ntrue AND true = " + (x && x));
        System.out.println("\n\tFor Logical OR...");
        System.out.println("false OR false = " + (y || y) + "\nfalse OR true = " + (y || x) + "\ntrue OR false = "
                + (x || y) + "\ntrue OR true = " + (x || x));
        System.out.println("\n\tFor Logical NOT...");
        System.out.println("NOT(false) = " + !y + "\nNOT(true) = " + !x);
    }
}

Output :

        For Logical AND...
false AND false = false
false AND true = false
true AND false = false
true AND true = true

        For Logical OR...
false OR false = false
false OR true = true
true OR false = true
true OR true = true

        For Logical NOT...
NOT(false) = true
NOT(true) = false

Bitwise Operators

  • These operators perform the operations on every bit of a number.
class Bitwise {
    public static void main(String[] arr) {
        System.out.println("\tFor Bitwise AND...");
        System.out.println("0 AND 0: " + (0 & 0));
        System.out.println("0 AND 1: " + (0 & 1));
        System.out.println("1 AND 0: " + (1 & 0));
        System.out.println("1 AND 1: " + (1 & 1));
        System.out.println("\n\tFor Bitwise OR...");
        System.out.println("0 OR 0: " + (0 | 0));
        System.out.println("0 OR 1: " + (0 | 1));
        System.out.println("1 OR 0: " + (1 | 0));
        System.out.println("1 OR 1: " + (1 | 1));
        System.out.println("\n\tFor Bitwise Ex-OR...");
        System.out.println("0 Ex-OR 0: " + (0 ^ 0));
        System.out.println("0 Ex-OR 1: " + (0 ^ 1));
        System.out.println("1 Ex-OR 0: " + (1 ^ 0));
        System.out.println("1 Ex-OR 1: " + (1 ^ 1));
    }
}

Output :

        For Bitwise AND...
0 AND 0: 0
0 AND 1: 0
1 AND 0: 0
1 AND 1: 1

        For Bitwise OR...
0 OR 0: 0
0 OR 1: 1
1 OR 0: 1
1 OR 1: 1

        For Bitwise Ex-OR...
0 Ex-OR 0: 0
0 Ex-OR 1: 1
1 Ex-OR 0: 1
1 Ex-OR 1: 0

Assignment Operators

  • These operators assign the result of operation (if any) to the left operand.
class Assignment {
    static double b = 6;

    public static void main(String[] arr) {
        double a = 4;
        b += a;
        System.out.println("Addition AND Assignment: " + b);
        b -= a;
        System.out.println("Subtraction AND Assignment: " + b);
        b *= a;
        System.out.println("Multiplication AND Assignment: " + b);
        b /= a;
        System.out.println("Division AND Assignment: " + b);
        b %= a;
        System.out.println("Modulus AND Assignment: " + b);
    }
}

Output :

Addition AND Assignment: 10.0
Subtraction AND Assignment: 6.0
Multiplication AND Assignment: 24.0
Division AND Assignment: 6.0
Modulus AND Assignment: 2.0

Precedence and Associativity of Operators in Java

The precedence of operator specifies that which operator will be evaluated first and next.
The associativity specifies the operator direction to be evaluated; it may be left to right or right to left.

CategoryOperatorsAssociativity
Postfix() [] {}Left to Right
Multiplicative* / %Left to Right
Additive+ -Left to Right
Shift<< >>Left to Right
Relational< <= > >=Left to Right
Equality== !=Left to Right
Bitwise AND&Left to Right
Bitwise XOR^Left to Right
Bitwise OR|Left to Right
Logical AND&&Left to Right
Logical OR||Left to Right
Assignment= += -= *= /=
%= >>= <<=
&= ^= |=
Right to Left

The + and - are equal in precedence, as *, /, and %.
The *, /, and % are performed first in order from left to right and then + and -, also in order left to right.

We can change the order of operations by using parenthesis () to indicate which operations are to be performed first.


Previous

Next

Comments