Operators in Java
let's talk about operators in java in order to perform some operation on values(operands) we need to specify what we actually going to perform like addition, subtraction, multiplication, etc. with the help of some symbols which are known as operators and they will return something after performing some operation so in java there are five types of operators which are listed below-
- Arithmetic operator
- bitwise operator
- logical operator
- assignment operator
- comparison operator
Arithmetic operator- It consists of addition(+), multiplication(*), subtraction(-) division(/) and modulo(%) let's check each of these in the code directly-
public class ArithmeticOperators {
public static void main(String[] args)
{
int a1=5;
int a2=6;
int a3 = a1+a2;
float a4 = 4.5f;
float a5 = 5.5f;
int a6 = (int)a4*(int)a5;
System.out.println("this is the value of a6"+a6);
System.out.println();//new line
int a7 = 5;
int a8 = 9;
int a9 = a7-a8;
int a10 = 12;
int a11 = 3;
int a12 = a10/a11;//return quotient
System.out.println("this is the value of a12"+a12);
System.out.println();//new line
int a13 = 17;
int a14 = 3;
int a15 = a13 % a14;//return remainder
System.out.println("this is the value of a15"+a15);
}
}
Output:
this is the value of a620
this is the value of a124
this is the value of a152
I know you are worrying about a6 so what happen at a6 actually is, I want to store the multiplication of a4 and a5 as an integer in a6 so here we have done individual type cast to a4 and a5 to integer (hope it is clear) and rest of the code is already clear to you because nothing more is there in it.
Bitwise operator-It includes operations like AND, OR, NOT, EXOR, RIGHT SHIFT and LEFT SHIFT let's see each of these directly in the code how they are implemented-
public class BitwiseOperators {
/* AND(&)
OR (|)
EXOR(^)
NOT(!)
RIGHT SHIFT(>)
LEFT SHIFT(<)
*/
public static void main(String[] args)
{
int a1=5;//101 in binary
int a2 =6;//110 in binary
int a3 = a1 & a2;
//(101 * 110(in binary) = 100(binary)=4 (in decimal))
System.out.println("the value of a3 is "+a3+"\n");
int a4=5;//101 in binary
int a5 =6;//110 in binary
int a6 = a4 | a5;
//(101 + 110(in binary) = 111(binary)=7 (in decimal))
System.out.println("the value of a6 is "+a6+"\n");
int a7 =16;//10000=16
int a8 = a7>>2;
//100=4(after n right shift last n bits will remove)
System.out.println("the value of a8 is "+a8+"\n");
int a9 =4;//100=4
int a10 = a9<<2;
//10000=16(after n left shift last n bits will add)
System.out.println("the value of a10 is "+a10+"\n");
int a11 =4;//100=4
int a12 =5;//101=5(100^101=001)
int a13 = a11^a12;
System.out.println("the value of a13 is "+a13+"\n");
int a14 =4;//100=4
int a15 =~a14;//011=3
System.out.println("the value of a15 is "+a15);//it will show you the 2's
//complement of 3 which is 5 with a negation sign
}
}
Output:
the value of a3 is 4
the value of a6 is 7
the value of a8 is 4
the value of a10 is 16
the value of a13 is 1
the value of a15 is -5
Comparison operator- In java comparison operators is of six types which are listed below-
- Equal to
- Not equal to
- less than
- greater than
- less than equal to
- greater than equal to
Comparison operator will return boolean type like true or false let's see how they are implemented directly in the code-
public class ComparisonOperators {
public static void main(String[] args)
{
int a=10;
int b=20;
System.out.println(a==b);
System.out.println(a!=b);
System.out.println(a>b);
System.out.println(a<b);
System.out.println(a>=b);
System.out.println(a<=b);
}
}
Output:
false
true
false
true
false
true
Assignment operator- They are used to assign values to a variable there are various types of assignment operators in java have a look on this table to get a more clear idea about assignment operators-
OPERATOR |
EXAMPLE |
EQUIVALENT TO |
= |
a=b |
a = b |
+= |
a+=b |
a = a + b |
-= |
a-=b |
a = a - b |
*= |
a*=b |
a = a * b |
/= |
a/=b |
a = a / b |
%= |
a%=b |
a = a % b |
>>= |
a>>=2 |
a = a >> 2 |
<<= |
a<<=2 |
a = a << 2 |
we will discuss each of them one by one directly in the code so go through the entire code-
public class AssignmentOperators {
public static void main(String[] args)
{
int a=10;
int b=a;
System.out.println(a+=2);
System.out.println(a-=2);
System.out.println(a/=2);
System.out.println(a*=2);
System.out.println(a>>=2);
System.out.println(a<<=2);
}
}
Output:
12
10
5
10
2
8
Logical operator-logical operators in java is of three types which are listed below:
logical AND (&&) ( Returns true if both statements are true )
logical OR (||) ( Returns true if either one of the statements are true )
logical NOT (!) (Reverse the result, returns false if the result is true)
let's see each of these directly in the code, how they are implemented and they also return boolean data type so without any more theory go straight to have a look on the code-
public class LogicalOperators {
public static void main(String[] args)
{
int a=5;
int b=7;
if(a==5 && b==6)
{
System.out.println("logical AND running");
}
if(a==5 || b==6)
{
System.out.println("logical OR running");
}
//logical NOT operator
System.out.println(!(a<b));//false
System.out.println(!(a>b));//true
}
}
Output:
logical OR running
false
true
Hello, visitor your comments are so helpful for us so do not hesitate just write the comment we read all your comments so don't think your comment goes waste.