Java Operators - Basics


In Java programming, operators are characters that represent specific mathematical or logical operations.They are used to perform operations on variables and values.

For Example, 

"+" is an arithmetic operator that indicates Addition, while "&&" is a logical operator representing the logical AND function in programming.

 

Operators  in Java are mainly divided into the following groups:

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

Arithmetic Operators

Using arithmetic operators, mathematical calculations are performed. In this case, operators take numerical values as operands and return one unique numerical value that implies there can be only one correct answer.

Operator Name Operation Example
+ Addition Returns sum of two numbers a+b
- Subtraction Subtracts the right-hand operand from the left a-b
* Multiplication Multiplies two numbers a*b
/ Division Divides one value by another a/b
% Modulus Computes the remainder a%b
 

Code Example

class ArithmeticOperator{
  public static void main(String[] args) {
    
    // Variable declaration
    int a = 12, b = 5;

    // addition operator
    System.out.println("a + b = " + (a + b));

    // subtraction operator
    System.out.println("a - b = " + (a - b));

    // multiplication operator
    System.out.println("a * b = " + (a * b));

    // division operator
    System.out.println("a / b = " + (a / b));

    // modulo operator
    System.out.println("a % b = " + (a % b));
  }
}

Output

a + b = 17
a - b = 7
a * b = 60
a / b = 2
a % b = 2

 

Assignment Operators

These operators are used to assign values to variables.

Operator Operation Example
= Assigns a value to any variable a=b
+= Add a value then assigns a+=b
-= Subtract a value then assign a-=b
*= Multiply and then assign a*=b
/= Divide the value then assign a/=b
 

Code Example

class AssignmentOperator{
  public static void main(String[] args) {
    
    int x = 4;
    int var;

    // assign value using =
    var = x;
    System.out.println("var using =: " + var);

    // assign value using +=
    var += x;
    System.out.println("var using +=: " + var);

    // assign value using *=
    var *= x;
    System.out.println("var using *=: " + var);
  }
}

Output

var using = : 4
var using += : 8
var using *= : 32

 

Comparison Operators

These operators are used for checking  relations like equality, greater than, and less than which return boolean results after the comparison and are mostly used in looping statements as well as conditional if-else statements.

Operator Operation Example
== Equal to a==b
!= Not equal to a!=b
< Less than a<b
> Greater than a>b
<= Less than equal to a<=b
>= Greater than equal to a>=b
 

Code Example

class ComparisonOperator{
  public static void main(String[] args) {
    
    int a = 7, b = 11;
    // value of a and b
    System.out.println("a = " + a + " and b = " + b);

    // == operator
    System.out.println("a == b : " + (a == b));  // returns false

    // != operator
    System.out.println("a != b : " + (a != b));  // returns true

    // > operator
    System.out.println("a > b : " + (a > b));  // returns false

    // < operator
    System.out.println("a < b : " + (a < b));  // returns true

    // >= operator
    System.out.println("a >= b : " + (a >= b));  // returns false

    // <= operator
    System.out.println("a <= b : " + (a <= b));  // returns true
  }
}

Output

a = 7 and b = 11
a == b : false
a != b : true
a > b : false
a < b : true
a >= b : false
a <= b : true

 

Logical Operators

These operators are used in decision making to check whether an expression is true or false.

Operator Name Operation Example
&& Logical AND True if both the expressions are true a && b
|| Logical OR True  if one of the two expression is true a||b
! Logical NOT True if expression is false and vice versa !a
 

Code Example

class LogicalOperator{
  public static void main(String[] args) {

    // && operator
    System.out.println("(8 > 3) && (9 > 6) : " + ((8 > 3) && (9 > 6)));  // true
    System.out.println("(5 > 3) && (8 < 5) : " + ((5 > 3) && (8 < 5)));  // false

    // || operator
    System.out.println("(5 < 3) || (8 > 5) : " + ((5 < 3) || (8 > 5)));  // true
    System.out.println("(5 > 3) || (8 < 5) : " + ((5 > 3) || (8 < 5)));  // true
    System.out.println("(5 < 3) || (8 < 5) : " + ((5 < 3) || (8 < 5)));  // false

    // ! operator
    System.out.println("!(3 == 4) : " + (!(3 == 4)));  // true
    System.out.println("!(9 > 3) : " + (!(9 > 3)));  // false
  }
}

Output

(8 > 3) && (9 > 6) : true
(5 > 3) && (8 < 5) : false
(5 < 3) || (8 > 5) : true
(5 > 3) || (8 < 5) : true
(5 < 3) || (8 < 5) : false
!(3 == 4) : true
!(9 > 3) : false

 

Bitwise Operators

These operators are used to perform the manipulation of individual bits of a number and can be used with any integer type.

Operator Name Operation Example
& Bitwise AND Returns bit by bit AND of input values a && b
| Bitwise OR Returns bit by bit OR of input values a|b
^ Bitwise XOR Returns bit by bit XOR of input values a^b
~ Bitwise Complement Returns one’s complement of input value ~a
 

Code Example

public class BitwiseOperators {
	public static void main(String[] args)
	{
		int a = 2;
		int b = 7;

		System.out.println("a = " + a + ", b = " + b);

		// bitwise AND
		System.out.println("a&b = " + (a & b));

		// bitwise OR
		System.out.println("a|b = " + (a | b));

		// bitwise XOR
		System.out.println("a^b = " + (a ^ b));

		// bitwise NOT
		System.out.println("~a = " + ~a);
		
	}
}

Output

a = 2, b = 7
a&b = 2
a|b = 7
a^b = 5
~a = -3

 

Unary Operators

Unary operators are used with only one operand

Operator Name Operation Example
++ Increment Increments the value by 1 a++ or ++a
Decrement Decrements the value by 1 a– or–a
! Logical complement Convert true to false and vice versa !a
 

Code Example

class UnaryOperators {
  public static void main(String[] args) {
    
    int a = 12, b = 12;
    int result1, result2;

    // original value
    System.out.println("Value of a: " + a);

    // increment operator
    result1 = ++a;
    System.out.println("After increment: " + result1);

    System.out.println("Value of b: " + b);

    // decrement operator
    result2= --b;
    System.out.println("After decrement: " + result2);

    boolean c = true;
    System.out.println("Value of c: " + c);

    c = !c;
    System.out.println("Logical complement of c : " + c);

  }
}

Output

Value of a: 12
After increment: 13
Value of b: 12
After decrement: 11
Value of c: true
Logical complement of c : false



Thanks for feedback.



Read More....
Java Exception Handling
HashMap in Java
Java : HashSet and LinkedHashSet
Inner classes in Java
Java Collections Framework
Java Data Types and Variables - Declaration and Initialization