Arithmetic operators
used in mathematical expressions and algebra expressions.
Basic arithmetic operators
Addition(+), Subtraction(-), Multiplication (*), Division (/), Modulus(%) used for both integer and float values also for char as it is a subset of int but not for Boolean type values.
Compound arithmetic assignment operators
+=, -=, *=, /= , %=
provides short hand form and efficient than long expressions.
syntax:
variable = variable + value;
variable += value;
Increment and Decrement operators
provides a special functionality of increment and decrement by 1.+
pre-fix: ++variable, --variable
post-fix: variable++, variable--
Difference between pre-fix and post-fix is
Ex:
int a=1,b=1,x,y;
x = ++a; // value of x is 2
y= b++; // value of y is 1
But the values of both a and b are 2.
//example program for arithmetic operators
class ArithmeticOperatorsExample
{
public static void main(String args[])
{
int a=1;
//basic arithmetic operators
int x = a+9; //10
int y = x-5; //5
int z = y*2; //10
int b = z/3;//3
int c = y%3;//2
System.out.println("basic arithmetic operators");
System.out.println("value of x is::"+x);
System.out.println("value of y is::"+y);
System.out.println("value of z is::"+z);
System.out.println("value of b is::"+b);
System.out.println("value of c is::"+c);
//compound assignment operators
x+=10; //20
y-=1; //4
z*=5; //50
b/=2; //1
c%=2; //0
System.out.println("compound assignment operators");
System.out.println("value of x is::"+x);
System.out.println("value of y is::"+y);
System.out.println("value of z is::"+z);
System.out.println("value of b is::"+b);
System.out.println("value of c is::"+c);
//increment and decrement operators
x=a++; //1 post-fix operator
y=++b; //2 pre-fix operator
System.out.println("increment and decrement operators");
System.out.println("value of x is::"+x);
System.out.println("value of y is::"+y);
System.out.println("value of a is::"+a);
System.out.println("value of b is::"+b);
}
}
Output
basic arithmetic operators
value of x is 10
value of y is 5
value of z is 10
value of b is 3
value of c is 2
compound assignment operators
value of x is 20
value of y is 4
value of z is 50
value of b is 1
value of c is 0
increment and decrement operators
value of x is 1
value of y is 2
value of a is 2
value of b is 2
Bitwise Operators
Bitwise logical operators
AND(&) ,OR(|), NOT(~), XOR(^)
Left Shift Operator
<< - shifts all the bits in the value to left by specified number of times.
value << number;
Right shift operator
>>- shifts all bits in the value by a specific number of bits to right.
value >> number;+
Unsigned right shift operator
>>>- fills higher order bit with its previous contents shifted.
value >>> number;
Bit wise operators compound assignment
&=, |=, ~=, ^=, <<=, >>=, >>>=.
//example program for arithmetic operators
class BitwiseOperatorsExample
{
public static void main(String args[])
{
int a=7,d=5;
//basic arithmetic operators
int x = a&d;
int y = a|d;
int z = ~d;
int b = a^d;
int c = a<<d;
int e = a>>2;
int f = a>>>2;
System.out.println("bitwise operators");
System.out.println("value of x is::"+x+" "+Integer.toBinaryString(x));
System.out.println("value of y is::"+y+" "+Integer.toBinaryString(y));
System.out.println("value of z is::"+z+" "+Integer.toBinaryString(z));
System.out.println("value of b is::"+b+" "+Integer.toBinaryString(b));
System.out.println("value of c is::"+c+" "+Integer.toBinaryString(c));
System.out.println("value of e is::"+e+" "+Integer.toBinaryString(e));
System.out.println("value of f is::"+f+" "+Integer.toBinaryString(f));
}
}
Output:
value of x is::5 101
value of y is::7 111
value of z is:: -6 111111111111111111111010
value of b is:: 2 10
value of c is::224 11100000
value of e is:: 1 1
value of f is::1 1
Relational operators
To find the relationship between integers, floating point numbers, characters, boolean values.
==, !=, <=, >=, <, >.
= is the assignment operator where as == is the operator to check the equality relation between int, float, char values.
The result of these operators is boolean value which we can used in if and loop expressions as it tells true or false.
in c,c++ we can write
if(!int_variable)
while(int_variable)
but in java we have to write like
if(int_variable==1)
if(int_variable!=0)
as in c,c++ the value of boolean variable true and false maps to 1 and 0.
but in java it is a separate data type which is not comparable with numeric values.
//example for Relational Operators
class RelationalOpeartorsExample{
public static void main(String []args)
{
int a=1;
if(a)
System.out.println("if condition using int data type"); //error as incompatible type: int can not convert to boolean
if(true)
System.out.println("if condition using int data type"); // it will executed.
}
}
Boolean Logical Operators
These are the logical operators which can perform on boolean operands as same as bit values.
logical AND &, logical OR |, logical XOR ^, logical unary NOT !, short-circuit AND &&, short-circuit OR ||, AND assignment &=, OR assignment |=, NOT assignment !=, equals to ==, NOT equals to !=, Ternary if-then-else ?:
logical AND, OR, NOT works as same as bitwise logical operations.
Short-Circute Logical Operators
AND (&&), OR(||)
//example fot boolean logical operators
class BooleanLogicalOperatorsExample
{
public static void main(String []args)
{
int a=0;
if(a!=0 & 10/a >10)
System.out.println("using logical AND & opearator");// error as we dividing 10 by 0
if(a!=0 && 10/1 >10)
System.out.println("it will not execute"); // this statement will not execute as if first expression is false it will not execute
}
}
logical AND will execute like it will execute both the expressions what ever the result of first expression while in short-circute AND operator will not execute the second expression if first one is false.
Assignment Opearator
single equals to = is the assignment opearator where we can assign a value to a variable of same data type.
syntax:
var = expression;
var and the result of expression should be of same data type.
- chain assignment in java
in java we can assign a value to a chain of variable in a single statement
ex:
int a,b,c =100;
Ternary Operator (?:)
It is the operator which can be used as a replace of a simple if-else condition.
syntax:
expression1? expression2: expression3.
expression1- is a boolean expression which results true or false.
expression2 and expression3 should not be a void type.
ex:
int n=5;
int a= n==0? 0: 10/n;
first it checks whether n equals to 0 or not.+
as n==5 it will executes the expression after column(:).
if n==0 it will execute the expression between ? and column:
//example for ternary operator
class TernaryOperatorExample
{
public static void main(String []args)
{
int n=0;
int a= n==0?0 : 10/5;
System.out.println("value of a is::"+a);
}
}
Operator Precedence
in java Arrow operator ---> is added in JDK8 which is used in lambda expressions.
(), [] , . operators have the highest precedence.
Highest
++ -- postifix
++ -- prefix ~ ! + - unary (typr-cast)
(*) / %
>>>>><<
(>) >= <<= instanceOf
== !=
&
^
|
&&
||
?:
-->
= op=
lowest
Using Parenthesis
using parenthesis in expressions will reduce the ambiguity and degrate the performance of program.
//example for using paranthesis
class ParanthesisExample
{
public static void main(String []args)
{
int a=5,b=4,c=3;
int x= a*(b+c);
int y = a*b+c;
int z= (a*b)+c;
System.out.println("value of x is::"+x);
System.out.println("value of y is::"+y);
System.out.println("value of z is::"+z);
}
}
Output:
value of x is:: 35
value of y is::23
value of z is::23