Post

Java 2 - Operators

Operators

  • The operators are almost the same in Java as in other programming languages.
  • Therefore if you have experience with another programming language, you will find Java operators familiar.
  • Operators are used to perform operations on variables and values.
  • Java has the following types of operators:
    • Arithmetic operators - used to perform arithmetic operations on variables and values
    • Assignment operators - used to assign values to variables
    • Comparison operators - used to compare two values
    • Logical operators - used to determine the logic between variables or values
    • Bitwise operators - used to perform bitwise operations
    • Ternary operators - used to evaluate a boolean expression
    • Instanceof operator - used to test if an object is an instance of a class

Arithmetic Operators

  • Arithmetic operators are used to perform common mathematical operations.
  • They are:
    • + - Addition
    • - - Subtraction
    • * - Multiplication
    • / - Division
    • % - Modulus
    • ++ - Increment
    • -- - Decrement

Assignment Operators

  • Assignment operators are used to assign values to variables.
  • They are:
    • = - Assigns values from right side operands to left side operand
    • += - It adds right operand to the left operand and assign the result to left operand
    • -= - It subtracts right operand from the left operand and assign the result to left operand
    • *= - It multiplies right operand with the left operand and assign the result to left operand
    • /= - It divides left operand with the right operand and assign the result to left operand
    • %= - It takes modulus using two operands and assign the result to left operand
    • <<= - It shifts left operand value to right by the number of bits specified by the right operand and assign the result to left operand
    • >>= - It shifts left operand value to right by the number of bits specified by the right operand and assign the result to left operand
    • &= - It performs ‘Bitwise AND’ on the operands and assign the result to left operand
    • ^= - It performs ‘Bitwise Exclusive OR’ on the operands and assign the result to left operand
    • |= - It performs ‘Bitwise Inclusive OR’ on the operands and assign the result to left operand

Comparison Operators

  • Comparison operators are used to compare two values.
  • They are:
    • == - Checks if the values of two operands are equal or not, if yes then condition becomes true
    • != - Checks if the values of two operands are equal or not, if values are not equal then condition becomes true
    • > - Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true
    • < - Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true
    • >= - Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true
    • <= - Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true

Comparing Strings

  • You cannot use the == operator to compare two strings.
  • Likewise the != operator.
  • You can use the equals() method to compare two strings.
1
2
3
4
5
6
7
public class MyClass {
    public static void main(String[] args) {
        String txt = "Hello";
        String txt2 = "Hello";
        System.out.println(txt.equals(txt2)); // true
    }
}
  • Instead of using != to compare two strings, you can use the ! operator to negate the result of the equals() method.
1
2
3
4
5
6
7
public class MyClass {
    public static void main(String[] args) {
        String txt = "Hello";
        String txt2 = "Hello";
        System.out.println(!txt.equals(txt2)); // false
    }
}
This post is licensed under CC BY 4.0 by the author.