Operators in Java
OPERATORS IN JAVA.
An administrator is a character that addresses an activity, for instance + is a number juggling administrator that addresses expansion.
Kinds of Operator in Java
1) Basic Arithmetic Operators
2) Assignment Operators
3) Auto-augmentation and Auto-decrement Operators
4) Logical Operators
5) Comparison (social) administrators
6) Bitwise Operators
7) Ternary Operator
1) Basic Arithmetic Operators
Fundamental math administrators are: +, - , *,/, %
+ is for expansion.
– is for deduction.
* is for duplication.
/is for division.
% is for modulo.
Note: Modulo administrator returns leftover portion, for instance 10 % 5 would bring 0 back
Illustration of Arithmetic Operators
public class ArithmeticOperatorDemo {
public static void main(String args[]) {
int num1 = 100;
int num2 = 20;
System.out.println("num1 + num2: " + (num1 + num2) );
System.out.println("num1 - num2: " + (num1 - num2) );
System.out.println("num1 * num2: " + (num1 * num2) );
System.out.println("num1/num2: " + (num1/num2) );
System.out.println("num1 % num2: " + (num1 % num2) );
}
}
Output:
num1 + num2: 120
num1 - num2: 80
num1 * num2: 2000
num1/num2: 5
num1 % num2: 0
Checkout these java programs identified with number juggling Operators in Java:
Java Program to Add two numbers
Java Program to Multiply two Numbers
2) Assignment Operators
Tasks administrators in java are: =, +=, - =, *=,/=, %=
num2 = num1 would allocate worth of variable num1 to the variable.
num2+=num1 is equivalent to num2 = num2+num1
num2-=num1 is equivalent to num2 = num2-num1
num2*=num1 is equivalent to num2 = num2*num1
num2/=num1 is equivalent to num2 = num2/num1
num2%=num1 is equivalent to num2 = num2%num1
Illustration of Assignment Operators
public class AssignmentOperatorDemo {
public static void main(String args[]) {
int num1 = 10;
int num2 = 20;
num2 = num1;
System.out.println("= Output: "+num2);
num2 += num1;
System.out.println("+= Output: "+num2);
num2 - = num1;
System.out.println("- = Output: "+num2);
num2 *= num1;
System.out.println("*= Output: "+num2);
num2/= num1;
System.out.println("/= Output: "+num2);
num2 %= num1;
System.out.println("%= Output: "+num2);
}
}
Output:
= Output: 10
+= Output: 20
- = Output: 10
*= Output: 100
/= Output: 10
%= Output: 0
3) Auto-augmentation and Auto-decrement Operators ++ and —
num++ is identical to num=num+1;
num– - is comparable to num=num-1;
Illustration of Auto-addition and Auto-decrement Operators
public class AutoOperatorDemo {
public static void main(String args[]){
int num1=100;
int num2=200;
num1++;
num2- - ;
System.out.println("num1++ is: "+num1);
System.out.println("num2- - is: "+num2);
}
}
Output:
num1++ is: 101
num2- - is: 199
4) Logical Operators
Coherent Operators are utilized with double factors. They are primarily utilized in contingent proclamations and circles for assessing a condition.
Legitimate administrators in java are: &&, ||, !
Suppose we have two boolean factors b1 and b2.
b1&&b2 will return valid if both b1 and b2 are genuine else it would return bogus.
b1||b2 will return bogus if both b1 and b2 are bogus else it would bring valid back.
!b1 would return something contrary to b1, that implies it would be valid if b1 is bogus and it would return bogus if b1 is valid.
Illustration of Logical Operators
public class LogicalOperatorDemo {
public static void main(String args[]) {
boolean b1 = valid;
boolean b2 = bogus;
System.out.println("b1 && b2: " + (b1&&b2));
System.out.println("b1 || b2: " + (b1||b2));
System.out.println("!(b1 && b2): " + !(b1&&b2));
}
}
Output:
b1 && b2: bogus
b1 || b2: valid
!(b1 && b2): valid
5) Comparison(Relational) administrators
We have six social administrators in Java: ==, !=, >, <, >=, <=
== returns valid if both the left side and right side are equivalent
!= returns valid whenever left side isn't equivalent to the correct side of administrator.
> returns valid whenever left side is more noteworthy than right.
< returns valid whenever left side is not exactly right side.
>= returns valid whenever left side is more noteworthy than or equivalent to right side.
<= returns valid whenever left side is not exactly or equivalent to right side.
Illustration of Relational administrators
Note: This model is utilizing if-else explanation which is our next instructional exercise, in the event that you are thinking that its hard to comprehend, allude if-else in Java.
public class RelationalOperatorDemo {
public static void main(String args[]) {
int num1 = 10;
int num2 = 50;
in the event that (num1==num2) {
System.out.println("num1 and num2 are equivalent");
}
else{
System.out.println("num1 and num2 are not equivalent");
}
in the event that( num1 != num2 ){
System.out.println("num1 and num2 are not equivalent");
}
else{
System.out.println("num1 and num2 are equivalent");
}
in the event that( num1 > num2 ){
System.out.println("num1 is more noteworthy than num2");
}
else{
System.out.println("num1 isn't more noteworthy than num2");
}
in the event that( num1 >= num2 ){
System.out.println("num1 is more noteworthy than or equivalent to num2");
}
else{
System.out.println("num1 is under num2");
}
in the event that( num1 < num2 ){
System.out.println("num1 is under num2");
}
else{
System.out.println("num1 isn't under num2");
}
in the event that( num1 <= num2){
System.out.println("num1 is not exactly or equivalent to num2");
}
else{
System.out.println("num1 is more prominent than num2");
}
}
}
Output:
num1 and num2 are not equivalent
num1 and num2 are not equivalent
num1 isn't more prominent than num2
num1 is under num2
num1 is under num2
num1 is not exactly or equivalent to num2
Look at these connected java programs identified with social administrators:
Java Program to check if number is positive or negative
Java Program to check whether number is even or odd
6) Bitwise Operators
There are six bitwise Operators: and, |, ^, ~, <<, >>
num1 = 11;/* equivalent to 00001011*/
num2 = 22;/* equivalent to 00010110 */
Bitwise administrator performs little by little preparing.
num1 and num2 analyzes relating pieces of num1 and num2 and creates 1 if the two pieces are equivalent, else it brings 0 back.
For our situation it would return: 2 which is 00000010 on the grounds that in the double type of num1 and num2 just second last pieces are coordinating.
num1 | num2 analyzes comparing pieces of num1 and num2 and produces 1 if either chomped is 1, else it brings 0 back. For our situation it would return 31 which is 00011111
num1 ^ num2 thinks about relating pieces of num1 and num2 and creates 1 on the off chance that they are not equivalent, else it brings 0 back.
In our model it would return 29 which is identical to 00011101
~num1 is a supplement administrator that simply changes the piece from 0 to 1 and 1 to 0. In our model it would return - 12 which is marked 8 digit comparable to 11110100
num1 << 2 is left shift administrator that moves the pieces to one side, disposes of the extreme left piece, and appoints the furthest right piece a worth of 0.
For our situation yield is 44 which is comparable to 00101100
Note: In the model beneath we are giving 2 at the correct side of this shift administrator that is the explanation pieces are moving two spots to one side.
We can change this number and pieces would be moved by the quantity of pieces indicated on the correct side of the administrator. Same applies to the correct side administrator.
num1 >> 2 is correct shift administrator that moves the pieces to one side, disposes of the extreme right piece, and allocates the furthest left piece a worth of 0.
For our situation yield is 2 which is comparable to 00000010
Illustration of Bitwise Operators
public class BitwiseOperatorDemo {
public static void main(String args[]) {
int num1 = 11;/* 11 = 00001011 */
int num2 = 22;/* 22 = 00010110 */
int result = 0;
result = num1 and num2;
System.out.println("num1 and num2: "+result);
result = num1 | num2;
System.out.println("num1 | num2: "+result);
result = num1 ^ num2;
System.out.println("num1 ^ num2: "+result);
result = ~num1;
System.out.println("~num1: "+result);
result = num1 << 2;
System.out.println("num1 << 2: "+result); result = num1 >> 2;
System.out.println("num1 >> 2: "+result);
}
}
Output:
num1 and num2: 2
num1 | num2: 31
num1 ^ num2: 29
~num1: - 12
num1 << 2: 44 num1 >> 2: 2
Look at this program: Java Program to trade two numbers utilizing bitwise administrator
7) Ternary Operator
This administrator assesses a boolean articulation and allot the worth dependent on the outcome.
Linguistic structure:
variable num1 = (articulation) ? esteem assuming valid : esteem assuming bogus
On the off chance that the articulation results valid, the principal esteem before the colon (:) is appointed to the variable num1 else the subsequent worth is doled out to the num1.
Illustration of Ternary Operator
public class TernaryOperatorDemo {
public static void main(String args[]) {
int num1, num2;
num1 = 25;
/* num1 isn't equivalent to 10 that is the reason
* the second worth after colon is relegated
* to the variable num2 */
num2 = (num1 == 10) ? 100: 200;
System.out.println( "num2: "+num2);
/* num1 is equivalent to 25 that is the reason
* the primary worth is alloted
* to the variable num2
*/
num2 = (num1 == 25) ? 100: 200;
System.out.println( "num2: "+num2);
}
}
Output:
num2: 200
num2: 100
Look at these connected java programs:
Java Program to discover Largest of three numbers utilizing Ternary Operator
Java Program to track down the littlest of three numbers utilizing Ternary Operator
Administrator Precedence in Java
This figures out which administrator should be assessed first if an articulation has more than one administrator. Administrator with higher priority at the top and lower priority at the base.
Unary Operators
++ – ! ~
Multiplicative
*/%
Added substance
+







0 Comments:
Post a Comment