Java Operators

 

Java Operators

Operators are used to perform operations on variables and values.

In the example below, we use the + operator to add together two values:

example :-


public class Main {

  public static void main(String[] args) {

    int x = 100 + 50;

    System.out.println(x);

  }

}

output:-
150


Java divides the operators into the following groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Bitwise operators

Arithmetic Operators

Arithmetic operators are used to perform common mathematical operations.



Java Assignment Operators

Assignment operators are used to assign values to variables.

In the example below, we use the assignment operator (=) to assign the value 10 to a variable called x:

example 1:-


public class Main {

  public static void main(String[] args) {

    int x = 10;

    System.out.println(x);

  }

}

The addition assignment operator (+=) adds a value to a variable:

example 2:-
public class Main {
  public static void main(String[] args) {
    int x = 10;
    x += 5;
    System.out.println(x);
  }
}

output:-

15

example 3:-

public class Main {
  public static void main(String[] args) {
    int x = 5;
    x -= 3;
    System.out.println(x);
  }
}

output:-
2



Java Comparison Operators

Comparison operators are used to compare two values:-

example 1:-


public class Main {

  public static void main(String[] args) {

    int x = 5;

    int y = 3;

    System.out.println(x == y); // returns false because 5 is not equal to 3

  }

}

output:-
false


example 2:-

public class Main {
  public static void main(String[] args) {
    int x = 5;
    int y = 3;
    System.out.println(x != y); // returns true because 5 is not equal to 3
  }
}

output:-

true.

example 3:

public class Main {
  public static void main(String[] args) {
    int x = 5;
    int y = 3;
    System.out.println(x > y); // returns true because 5 is greater than 3
  }
}


output:-
true 


example 4:-

public class Main {
  public static void main(String[] args) {
    int x = 5;
    int y = 3;
    System.out.println(x < y); // returns false because 5 is not less than 3
  }
}


output:
false.


example 5:-


public class Main {
  public static void main(String[] args) {
    int x = 5;
    int y = 3;
    System.out.println(x >= y); // returns true because 5 is greater, or equal, to 3
  }
}


output:-

true.


example 6:-

public class Main {
  public static void main(String[] args) {
    int x = 5;
    int y = 3;
    System.out.println(x <= y); // returns false because 5 is neither less than or equal to 3
  }
}

output:-

false.




Java Logical Operators

Logical operators are used to determine the logic between variables or values:


example 1:-


public class Main {

  public static void main(String[] args) {

    int x = 5;

    System.out.println(x > 3 && x < 10); // returns true because 5 is greater than 3 AND 5 is less than 10

  }

}



output :-
true.


example 2:-

public class Main {
  public static void main(String[] args) {
    int x = 5;
    System.out.println(x > 3 || x < 4); // returns true because one of the conditions are true (5 is greater than 3, but 5 is not less than 4)
  }
}


output:-

true.

example 3:-
public class Main {
  public static void main(String[] args) {
    int x = 5;
    System.out.println(!(x > 3 && x < 10)); // returns false because ! (not) is used to reverse the result
  }
}


output:

fasle.


Previous
Next Post »