Operators in JAVA Part 2
Relational operators:
- ==
- !=
- <
- <=
- >
- >=
// RelationalOperatorsTest.java
public class RelationalOperatorsTest {
public static void main(String rags[]) {
int a=25;
int b=75;
double x=35.87;
double y=67.43;
char c1='b';
char c2='d';
/*
Relational operators can be applied only on primitive data types but not on arrays and objects. byte, short, int, long, float, double, char, Boolean are primitive data types.
*/
if(a==b) {
System.out.println("a==b is true");
} else {
System.out.println("a==b is false");
}
if(a!=b) {
System.out.println("a!=b is true");
} else {
System.out.println("a!=b is false");
}
if(a>=b) {
System.out.println("a>=b is true");
} else {
System.out.println("a>=b is false");
}
if(a<=b) {
System.out.println("a<=b is true");
} else {
System.out.println("a<=b is false");
}
}
}
Ternary Operator (?:):
- A simple if-else condition with one-one statement is called as Ternary Operator
- Ternary operator operates on three operands. One of its use is assign the minimum or maximum value of two variables to a third variable.
- Simple if-else condition
if-else contains only one-one statement that to the statement must return a value
// TernaryOperatorDemo.java
public class TernaryOperatorDemo {
public static void main(String rags[]) {
int a=126; int b=70; int d=100;
int c=0;
if(a<b) {
c=a;
} else {
c=b;
}
c=(a<b)?a:b;
System.out.println(c);
String str=(a<b)?"a is less than b":"b is less than a";
System.out.println(str);
String name="Mr. Bean", email="", mobile="", address="";
String err=(name.isEmpty())?"SName cannot be null\n":"";
err=err+(email.length()==0)?"Email cannot be null\n":"";
err+=(mobile.isEmpty())?"Mobile cannot be null\n":"";
err+=(address.isEmpty())?"Address cannot be null\n":"";
if(err.length()>0) {
System.out.println(err);
}
c=(a<b)?a:(b<d)?b:d;
System.out.println(c);
}
}
Bitwise Operators:
- Bitwise operators are used to manipulate individual bits of a data item. There are situations where individual bits of data are modified. These operators operate only on byte, short, int and long types.
Operator Description
~ Bitwise NOT
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
Shift operators:
<< Left shift
>> Right shift
>>> Right shift zero fill
& Operator
- Bitwise AND (&) operator compares each bit of first operand with each bit of second operand. For instance 71 binary number is 01000111 and 25 binary number is 00011001.
- If both the bits are 1 and 1 the result is 1, if any one of the bit is 0 the result is 0.
71 01000111
25 00011001
-------------------------------------
1 00000001
byte a=71;
byte b=25;
byte c=a&b;
System.out.println(c); // result is 1
| Operator
- Bitwise OR operator compares each bit of first operand with each bit of second operand. If the bits of both operands are 1 the result is 1, even if any one of the operand is 1 then the result is 1 and only of both operand bit values are 0 then the result is 0.
71 01000111
25 00011001
-------------------------------------
95 01011111
^ Bitwise Exclusive OR Operator
- Bitwise Exclusive OR Operator compares each bit of first operand with each bit of second operand. If bits of both operands are 0 or 1 then the result is 0. If any one of the operand bit is 1 and other operand is 0 then the result is 1.
int k=i^j;
71 01000111
25 00011001
------------------------------------
94 01011110
~ Operator
- Bitwise NOT operator is a unary operator. This operator is used only on a single operand. 71 binary value is 01000111.
71 01000111
~71 10111000
// BitwiseTest.java
public class BitwiseTest {
public static void main(String rags[]) throws Exception {
// bitwise AND (&)
int a=71, b=25;
int c=a&b;
System.out.println("c is "+c);
// bitwise inclusive OR (|)
int d=71, e=25;
int f=d|e;
System.out.println("f is "+f);
// bitwise exclusive OR (^)
int g=71, h=25;
int i=g^h;
System.out.println("i is "+i);
// bitwise NOT (~)
int j=71;
int k=~j;
System.out.println("k is "+k);
}
}
Shift Operators:
<< Operator (Left shift operator)
- The left shift operator is used to shift the bits of a given operand to left for the specified number of positions.
- For instance 71 binary value is 01000111, so if we declare a variable:
int a =71;
shift binary value of the 71 to two bits left.
int b=a<<2;
binary value of 71 is 01000111
shift 2 bits left 00011100
The value of 00011100 is 28
>> Operator (Right shift operator)
- The right shift operator is used to shift the bits of a given operand to right for the specified number of positions.
- One most important point is when bits are shifted to right and the left hand side bits are replaced with 0s in case of positive integer values and left hand side bits are replaced with 1s in case of negative integer value.
For instance 71 binary value is 01000111, so if we declare a variable:
int a =71;
shift binary value of the 71 to two bits right.
int b=a>>2;
binary value of 71 is 01000111
shift 2 bits right 00010001
The value of 00010001 is 17.
In case of negative integer value:
int a =-25;
the binary value is 10011001
if we apply int b=a>>2;
the resultant binary value is:
11100110
For each bit value 1 is appended at the left hand side indicates that the value is negative integer value.
>>> Operator (Right shift zero fill operator)
- This operator performs the same type of operation performed by right shift operator except that sign bit extension is not one 1. Instead of 1s filled at left most bit in vase of –ve integer value 0s are filled.
- This is equivalent to right shift operator with +ve integer value but fills 0 at left hand side in case of –ve integer value.
- In case of negative integer value:
int a=-25;
the binary value is 10011001
if we apply int b=a>>>2;
the resultant binary value is
00100110
For each bit value 0 is appended at the end of left hand side though it is negative integer value because of right shift zero fill operator.
// ShiftOperatorsTest.java
public class ShiftOperatorsTest {
public static void main(String rags[]) throws Exception {
// left shift operator
int a=25; int b=a<<2;
System.out.println("a is "+a);
System.out.println("a<<2 is "+b);
// right shift operator
a=25; b=a>>2;
System.out.println("a is "+a);
System.out.println("a>>2 is "+b);
// right shift 0 fill operator for –ve integer values
a=-25; b=a>>>2;
System.out.println("a is "+a);
System.out.println("a>>>2 is "+b);
System.out.println(Integer.toBinaryString(25));
System.out.println(Integer.toOctalString(25));
System.out.println(Integer.toHexString(25));
}
}
2 Comments
The java script what ever you have written is good and easy to understand
ReplyDeleteYes this is correct
ReplyDelete