Java Operators

Operators in java

Operators are symbols used to perform operations on variables and values in Java. They serve various purposes, from mathematical calculations to logical comparisons.

Operator Precedence: Operators in Java have a specific order of execution, known as precedence. Understanding this order is crucial to writing correct expressions.

Java divides the operators into the following groups:

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

Arithmetic Operators

Arithmetic operators allow you to perform mathematical operations like addition, subtraction, multiplication, division, and modulus (remainder).

OperatorNameDescriptionExample
+AdditionAdds together two valuesx + y
-Subtraction Subtracts one value from anotherx - y
*Multiplication Multiplies two valuesx * y
/Division Divides one value by anotherx / y
%ModulusReturns the division remainder x % y
++IncrementIncreases the value of a variable by 1++x
--DecrementDecreases the value of a variable by 1--x

Assignment Operators

Assignment operators ( = ) are used to assign values to variables. Compound assignment operators combine an operation and assignment in one step.

A list of all assignment operators:

OperatorExampleSame As
=x = 5x = 5
+=x += 6x = x + 6
-=x-=9x=x-9
*=x*=6x=x*6
/=x/=6x=x/6
%=x%=6x=x%6
&=x&=6x=x&6
|=x|=6x=x|6
>>=x >>= 3x = x >> 3
<<=x <<= 3x = x << 3

Comparison Operators

Comparison operators are used to compare values and return boolean results, indicating whether the comparison is true or false.

Exaple
int x = 5;
int y = 3;
System.out.println(x > y);  // output: true
OperatorNameExample
==Equal toExample
!=Not equalx != y
>Greater thanx > y
<Less thanx < y
>=Greater than or equal tox >= y
<=Less than or equal tox <= y

Logical Operators

Logical operators are used to perform logical operations, like AND, OR, and NOT, primarily in conditional statements.

Example
int a=5;
int b=5;
if( a==b && b==3){
        System.out.println(" Abyantrik");
}else {
       System.out.println("Tutorials");
}   // output: Tutorials
OperatorNameDescriptionExample
&& Logical andReturns true if both statements are truex < 5 && x < 10
|| Logical orReturns true if one of the statements is truex < 5 || x < 4
!Logical not Reverse the result, returns false if the result is true!(x < 5 && x < 10)

Bitwise Operators

Bitwise operators work on individual bits of integers. They are used in low-level programming and advanced operations.

OperatorsSymbolExample
Bitwise AND&x & y
Bitwise OR|x | y
Exclusive OR (XOR)^x ^ y
Bitwise Left shift<<x << y
Bitwise Right shift>>x >> y

Conditional (Ternary) Operator

The ternary operator is a shorthand way of writing if-else statements, allowing you to make decisions in a compact form. ( ? , :)

Syntax
(condition) ? "Statement execute if true": "Statement execute if false";

The instanceof operator is used to determine the type of an object, helping you work with polymorphism and object-oriented programming.

Contact Us

Name
Email
Mobile No:
subject:
Message: