Categories: Java

Operators in JAVA

The operators in Java represent the operations, i.e, operators perform operations on the operands.

Let us discuss the various operators in Java.

Arithmetic Operators:
  • These operators are for basic arithmetic operations
  • They are additions, subtraction, multiplication, division, and remainder.
  • Each of these operators is binary, i.e, it requires two values (operands) to calculate the final answer.
OperatorsDescription
+adds two operands, example:
int a=2,b=4;
int c=a+b;

output: c=6
subtract two operands, example:
int a=2,b=4;
int c=b-a;

output: c=2
*multiply two operands, example:
int a=2,b=4;
int c=a*b;

output: c=8
/divides two operands and provides their quotient, example:
int a=2,b=4;
int c=b/a;

output: c=2
%divides two operands and provides their remainder, example:
int a=2,b=4;
int c=b%a;

output: c=0
Unary Operators:
  • Operators that act on one operand are known as unary operators.
  • incrementing/decrementing a value by one
  • negating an expression
  • inverting the value of a boolean
Operator CategoryDescription
++, – –prefixIncrement and decrement
example:
int x=5,y=3;
++x; –y;
output:
x=6 y=2;
postfixIncrement and decrement
example:
int x=5,y=3;
x++; y–;
output:
x=6 y=2;
~ prefixnegate an expression
example:
int a=8;
~a;
output:a=-9
!prefixinvert boolean value.
example:
boolean a=true;
!a;
output: a=false
Binary Operators:
  • Operators that act upon two operands are referred to as Binary Operators.
  • The operands of the binary operator are distinguished as left or right operands
  • These are the same as the Arithmetic operators.
Logical Operators:
  • These are used to check conditions.
  • These are of six types. They are:
OperatorUseReturns true if
&&x && yx and y are both true, conditionally evaluates y
||x || yeither x or y is true, conditionally evaluates y
!!xx is false.
&x and y are both true, and always evaluate x and y.
|x | yeither x or y is true, always evaluates x and y.
^x ^ yif x and y are different – that is if one or the other of the operands is true but not both.
Relational Operator:
  • These operators represent the relationship that operands can have.
  • Java provides six relational operators.
  • It returns a boolean value.
OperatorUseDescription
comparison<true if right operand is greater than left operand, otherwise false.
>true if left operand is greater than right operand, otherwise false.
<= true if right operand is greater or equal to left operand, otherwise false.
>= true if left operand is greater or equal to right operand, otherwise false.
equality==returns true if both operands are equal, otherwise false.
!=returns if both operands are not equal, otherwise false.
Assignment Operator:
  • These operands allow us to assign one value to another operand;
Assignment= += -= *= /= %= &= ^= |= <<= >>= >>>=
  • The +=, -=, *=, etc are called shorthand operators which simplify the code as it assigns as well as evaluates the value. For instance,
x+=10; //this is same as x=x+10;
Shift Operators:
  • Shift operators perform bit manipulation on data by shifting the bits of its first operand right or left.
OperatorUseDescription
>>x>>yshift bits of x right by y bits
<<x<<yshift bits of x left by y bits
>>>x>>>yFor positive number, >> and >>> works same
For negative number, >>> changes parity bit (MSB) to 0
Bitwise Operator:
  • The bitwise operators work with integral types
  • They are sometimes referred to as logical operators when working with boolean values.
OperatorUseDescription
&x&yBitwise and
|x|ybitwise or
^x^ybitwise xor
~~ybitwise complement
Other Operators:
OperatorDescription
? :Shortcuts if-else statement
[]Used to declare arrays, create arrays, and access array elements
.Used to form qualified names
(params)Delimits a comma-separated list of parameters
(type)Casts(converts) a value to a specified type
newCreates a new object or a new array
instanceofDetermines whether its first operand is an instance of its second operand

Note: also read about the Byte Code in Java

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago