Tutorials Hut

Java Operators and Types

Java operators covers types and details of various mathematical and logical operations, we use in java programming.

Simple example can be addition (+), subtraction(-) and multiplication(*) (mathematical operators) which we learned in mathematics same is available in java for doing calculations.

Below is list of different java basic operators categories:

  • Arithmetic operators in java
  • Relational operators in java
  • Logical operators in java
  • Bitwise operators in java
  • Type and conditional operator

Arithmetic Java Operators

Arithmetic operations are simple mathematical operations between two numbers.

In below table we are seeing example with int primitive variable type, we can use these operators with float, double etc. as well.

Operator symbol and nameDetailsJava Examples
+ (Addition operator)Used to add two numeric variables.int a=2, b=20;
int c = a + b;
//if we print value of c, it //will be 22
– (Subtraction)Used to subtract two numeric variables.int a=2, b=20;
int c = b-a;
//if we print value of c, it //will be 18
* (Multiplication)Used to multiply two variables.int a=2, b=20;
int c = b*a;
//if we print value of c, it //will be 40
/ (Division)Divides two variable.int a=2, b=20;
int c = b/a;
//if we print value of c, it //will be 10
% (Modulus)Divides two variables and returns remainder.int a=2, b=21;
int c = b%a;
//if we print value of c, it //will be 1
++ (Increment)Increases variable’s value.int a=2;
a++;
//if we print value of c, it //will be 3
– -(Decrement)Decreases variable’s value.int a=2;
a–;
//if we print value of c, it //will be 1

Relational Java Operators

Relational operators are used to compare two variables and check which one is less or greater or equal.

Operator Symbol and NameDetailsJava Examples
== (equal equal)Used to compare two variables equality, returns true or false.int a=20, b=20;
if(a==b)
//if evaluate, if condition, it will return true.
!= (not equal)Used to compare if two variables values are not equal.int a=2, b=20;
if(a!=b)
//if evaluate, if condition, it will return true.
>(Greater than operator)Used to check if left side variable is greater than right side.int a=2, b=20;
if(b>a)
//if evaluate, if condition, it will return true.
<(Less than)Used to check if left side variable is less then right side.int a=2, b=20;
if(a //if evaluate, if condition, it will return true.
>= (Greater than equal)Used to check if left side variable is greater then or equal to right side variable.int a=2, b=20;
if(a>=b)
//if evaluate, if condition, it will return false.
<=(Less than equal)Used to check if left side variable is less than or equal to right side variable.int a=2, b=20;
if(a<=b)
//if evaluate, if condition, it will return true.

Logical Java Operators

Logical operators are used for some logical calculation between two variables.

See below table to understand them:

Operator Symbol and NameDetailsJava Examples
&&(AND operator)Used to perform logical AND operation between left and right side of expressions.if(20==20 && 30==30)

//if we carefully see this expression, we will get true in this if condition because left side and right side of && are true
|| (OR operator)Used to perform logical OR operation between left and right side of expressions.if(20==20 || 20==30)

//if we carefully see this expression, we will get true , even if 2nd condition is false.
‘! (NOT operator)Used to perform logical not operation.if(20!=30)

//if we carefully see this expression, we will get true as 20 is not equal to 30

Bitwise Operators

Bitwise operators are used to do operations on binary numbers (0/1), so if you apply a bitwise operation on two int, it will actually convert these two int numbers into binary number internally and then will do bitwise operation.


For example int 1 (0001 in binary) and int 2 (0010 in binary), let’s see some bitwise operations:

int a=1; //(0001)
int b=2; //(0010)
a&b = 0000

Operator Symbol and NameDetailsJava Examples
&(bitwise and operator)It does bitwise and operation on binary value of number.Int a=1, b=2;
Int c = a&b
| (or operator)It does bitwise or operation on binary value of number.
^ (XOR)It does bitwise xor operation on binary value of number.
~ (bitwise compliment)It is ones complement, flips the bits from 1 to 0 and 0 to 1.
<< (left shift)The left side value is moved to left by number of times specified in right side value.
>>(right shift)The left side value is moved to right by number of times specified in right side value.
>>>(zero fill right shift)The left side value is moved right by number of bits specified by left one and shifted values are flipped.

Assignment Operators

Assignment operators are used for assigning some value to a variable.
Operator Symbol and NameDetailsJava Examples
!ERROR! undefined variable ‘equals’Used to assign left variable value of right one.Int a=1, b=2;
+=It is used to add on left variable with right and assign to left.
-=It is used to substract on left variable with right and assign to left.
*=It is used to multiply on right variable with right and assign to left.
/=It is used to divide on left variable with right and assign to left.
%=It returns modulus of left and right data and assigned to left.
<<=Left shift AND operator.
>>=Right shift and assignment operator.
&=Bitwise AND and assignment operator.
!=Bitwise OR and assignment operator.

Type And Conditional Operator

· instanceof – Checks if left side object is instance of right side class, returns true or false.
Syntax: instanceof

· ?: (Conditional) – It is used to check a condition and return in if else way some value.
Syntax : (Condition/Expression)? :
Example: String message = (20>10)? “Yes” : “No”

Next Article To Read

Now you have great understanding of various operators in java, next you can read our article where we will discuss different java loops:














  • Leave a Reply

    Your email address will not be published. Required fields are marked *