Java Operators

In this tutorial you will learn about the Java Operators and its application with practical example.

Java Operators

An operator is a special symbol that is used to carry out some specific operation on its operand. In Java, we have rich set of built in operators to carry out different type of operations. There are operators for assignment, arithmetic operations, logical operations and comparison operations etc. Operators can be used with many types of variables or constants, but some of the operators are restricted to work on specific data types. Most operators are binary, meaning they take two operands, but a few are unary and only take one operand.

Type of operators in Java

In Java, we have following types of operators available –

  • Assignment Operators
  • Arithmetic Operators
  • Unary Operators
  • Relational Operators
  • Logical Operators
  • Conditional Operators
  • Bitwise Operators
  • instanceof Operators
  • Dot (.) Operator

Java Assignment Operators

Assignment operators are used to assign value to a variable, you can assign a variable value or the result of an arithmetical expression. In many cases assignment operator can be combined with other operators to build a shorthand version of a assignment statement are known as Compound Statement. For example, instead of a = a+5 , we can write a += 5.

Java Assignment operators
Operator Description Expression
= Assignment Operator
a=b
+= add and assign a+=b is equivalent to a=a+b
-= subtract and assign a-=b is equivalent to a=a-b
*= multiply and assign a*=b is equivalent to a=a*b
/= divide and assign a/=b is equivalent to a=a/b
%= mod and assign a%=b is equivalent to a=a%b
<<= Left shift AND assign a<<=5 is equivalent to a=a<<5
>>= Right shift AND assign a>>=5 is equivalent to a=a>>5
&= Bitwise AND assign a&=5 is equivalent to a=a&5
^= Bitwise exclusive OR and assign a^=5 is equivalent to a=a^5
|= Bitwise inclusive OR and assign a|=5 is equivalent to a=a|5

 

Recommended:-  Java Keywords and Identifiers

Java Arithmetic Operators

Arithmetic Operators are used to perform arithmetic operations like addition, subtraction, multiplication, division, %modulus, exponent, etc. Let variable a holds 20 and variable b holds 10, then −

Java Arithmetic operators
Operator Name Description Example
+ Addition Addition of given operands
a+b returns 30
- Subtraction Subtraction of second operand from first a-b returns 10
* Multiply Multiplication of given operands a*b returns 200
/ Division Returns Quotient after division
a/b returns 2
% Modulus Returns Remainder after division a%b returns 0

 

Java Unary Operators (post and pre)

In Java, ++ and — are know as increment and decrement operators respectively. These are unary operators it means they works on single operand. ++ adds 1 to operand and — subtracts 1 to operand respectively. When ++ is used as prefix(like: ++i), ++i will increment the value of i and then return it but, if ++ is used as postfix(like: i++), operator will return the value of operand first and then only increment it.

Operator Example Description
++ [prefix] ++a The value of a after increment
++ [postfix] a++ The value of a before increment
— [prefix] –a The value of a after decrement
— [postfix] a– The value of a before decrement

Java Comparison (Relational) Operators

Comparison Operators are used evaluate a comparison between two operands. The result of a comparison operation is a Boolean value that can only be true or false. Comparison Operators are also referred as relational operators.

Let variable a holds 20 and variable b holds 10, then −

Java Relational operators
Operator Description Example
> greater than a>b returns TRUE
< Less than a<b returns FALSE
>= greater than or equal to a>=b returns TRUE
<= less than or equal to a<=b returns FALSE
== is equal to a==b returns FALSE
!= not equal to a!=b returns TRUE

 

Recommended:-  Java if else statement

Java Logical Operators

Logical operators are used to combine expressions with conditional statements using logical (AND,OR,NOT) operators, which results in true or false. Let variable a holds true or 1 and variable b holds false or 0, then −

Java Logical operators
Operator Name Description Example
&& Logical AND return true if all expression are true (a && b) returns false
|| Logical OR return true if any expression is true (a || b) returns true
! Logical NOT return complement of expression !a returns false

Java Conditional operator ( ? : )

In Java, conditional operator is considered as short hand for if-else statement. Conditional operator is also called as “Ternary Operator”.

Syntax:

If condition is true the expression will return result1, if it is not it will return result2.

Java Bitwise Operators

Bitwise operator are used to perform bit level operation over its operand.

Let A = 60; and B = 13;

Binary equivalent

A = 0011 1100

B = 0000 1101

Operator Meaning Example Description
& Binary AND (A & B) It returns 12 which is 0000 1100
| Binary OR (A | B) It returns 12 which is 0000 1100
^ Binary XOR (A ^ B) It returns 49 which is 0011 0001
~ Ones Complement (~A ) It returns -60 which is 1100 0011
<< shift left A << 2 It returns 240 which is 1111 0000
>> shift right A >> 2 It returns 15 which is 0000 1111

Java instanceof Operator

In Java, the instanceof Operator is used to determine whether an object is an instance of a class, a subclass or an interface or not.

Recommended:-  Java Compile and Run Program

Java Dot(.) Operator

The (.) operator is also known as member operator it is used to access the member of a package or a class.

In this tutorial we have learn about the Java Operators and its application with practical example. I hope you will like this tutorial.