In programming, data processing and manipulation is highly dependent on the operators available. The operators in Java can be divided into the following groups:
The operations for which a unary operator is applicable are:
Let us look into the operators
Operator Name | Expression |
---|---|
Pre-increment/Pre-decrement | ++value, --value |
Post-increment/Post-decrement | value++ ,value-- |
Inversion | ~value |
Negation | !value |
In the case of pre-increment/decrement, the value is incremented/decremented by 1, saved into memory, and then used, wherever required.
public class MyOperators{
public static void main(String []args){
int myInt = 5;
int myInt2 = 10;
System.out.println("Value of myInt is "+myInt);
++myInt;
System.out.println("Value of myInt after pre increment is "+ myInt);
System.out.println();
System.out.println("Value of myInt2 is "+myInt2);
--myInt2;
System.out.println("Value of myInt after pre decrement is "+myInt2);
}
}
Value of myInt is 5
Value of ++myIntis 6
Value of myInt2 is 10
Value of --myInt2 is 9
In case of post-increment/decrement, is first used wherever required, and then the value is incremented/decremented by 1, saved into memory.
public class MyOperators{
public static void main(String []args){
int myInt = 5;
int myInt2 = 10;
System.out.println("Value of myInt is "+myInt);
myInt++;
System.out.println("Value of myInt after post increment is "+ myInt);
System.out.println();
System.out.println("Value of myInt2 is "+myInt2);
myInt2--;
System.out.println("Value of myInt after post decrement is "+myInt2);
}
}
Value of myInt is 5
Value of ++myIntis 6
Value of myInt2 is 10
Value of --myInt2 is 9
We obtain the same result. Then what is the difference? Let us look deep into it. In the below example we are incrementing the value using operator while printing only.
public class MyOperators{
public static void main(String []args){
int myInt = 5;
int myInt2 = 10;
System.out.println("Value of myInt is "+myInt);
System.out.println("Value of myInt after pre increment is "+ ++myInt);
System.out.println();
System.out.println("Value of myInt2 is "+myInt2);
System.out.println("Value of myInt after pre decrement is "+ --myInt2);
//Changing it back to the original value
myInt = 5;
myInt2= 10;
System.out.println("\nValue of myInt is "+myInt);
System.out.println("Value of myInt after post increment is "+ myInt++);
System.out.println();
System.out.println("Value of myInt2 is "+myInt2);
System.out.println("Value of myInt after post decrement is "+ myInt2--);
System.out.println("\nIn case of post, after being used in the above print statement, the value was changed and saved in the memory");
System.out.println("\nValue of myInt is "+ myInt);
System.out.println("Value of myInt2 is "+ myInt2);
}
}
Value of myInt is 5
Value of myInt after pre increment is 6
Value of myInt2 is 10
Value of myInt after pre decrement is 9
Value of myInt is 5
Value of myInt after post increment is 5
Value of myInt2 is 10
Value of myInt after post decrement is 10
In case of post, after being used in the above print statement,the value was changed and saved in the memory
Value of myInt is 6
Value of myInt2 is 9
As you can see, the result shows all. The operand with post-increment/decrement is first used and then incremented/decremented, while in case of pre-increment/decrement, the value is first changed and then used. As a result, the change was visible right away in the case of pre, but not in post.
As the name indicates, the inversion operator inverts the sign of the operand after incrementing the value.
public class MyOperators{
public static void main(String []args){
int myInt = 5;
System.out.println("myInt = "+myInt);
System.out.println("~myInt ="+~myInt);
}
}
myInt = 5 ~myInt =-6
The negation operator simply toggles the Boolean value i.e true becomes false and vice versa.
public class MyOperators{
public static void main(String []args){
boolean myBool = false;
System.out.println("myBool = "+myBool);
System.out.println("!myBool = "+!myBool);
}
}
myBool = false !myBool = true
The operations for which binary operators are applicable are:
* , / , % , + , -
)<< , >> , >>>
)< , > , <= , >= , instanceof , == , !=
)& , ^ , |
)&& , ||
) * , / , % , + , -
):The arithmetic operator acts as a basic mathematical model. The following example will be self-explanatory:
public class MyOperators{
public static void main(String []args){
int myInt = 30;
int myInt2 = 6;
System.out.println("myInt = "+myInt);
System.out.println("myInt2 = "+myInt2);
//This is a division operator. It returns myInt*myInt2 i.e 180
System.out.println("myInt * myInt2 = "+ (myInt * myInt2));
//This is a division operator. It returns myInt/myInt2 i.e 5
System.out.println("myInt / myInt2 = "+ (myInt / myInt2));
//This is a modulo operator. It returns the remainder of myInt/myInt2 i.e 30%6 has no remainder. Hence, 0
System.out.println("myInt % myInt2 = "+ (myInt % myInt2));
//This is a addition operator. It returns myInt+myInt2 i.e 36
System.out.println("myInt + myInt2 = "+ (myInt + myInt2));
//This is a minus operator. It returns myInt-myInt2 i.e 24
System.out.println("myInt - myInt2 = "+ (myInt - myInt2));
System.out.println();
System.out.println("The value of expression 30/6*2-7+8%4 is "+(30/6*2-7+8%4));
}
}
myInt = 30 myInt2 = 6 myInt * myInt2 = 180 myInt / myInt2 = 5 myInt % myInt2 = 0 myInt + myInt2 = 36 myInt - myInt2 = 24 The value of expression 30/6*2-7+8%4 is 3
One more interesting thing about the + operator is that it is applicable to strings as well. The + operator concatenates two string.
<< , >> , >>>
):Say we have operand 7 and 2 for every shift operator i.e 7<<2, 7>>2, 7>>>2
)
The Shift operator works in the following steps for the given operands:
<<
(left shift ), the digits shift to the left by 2 positions. After shifting, the void spaces are filled with 0. >>
(signed right shift ), the digits shift to the right by 2 positions. After shifting, the void spaces are filled with 0 for positive integers and 1 for negative integers. In our case 7 is positive, hence it is to be filled with 0. ( 111 after <<2, turns into **1, the void spaces are filled with 0, hence it is 001)>>>
(unsigned right shift ), the digits shift to the right by 2 positions. After shifting, the void spaces are filled with 0 always, irrespective of the sign of the number.
public class MyOperators{
public static void main(String []args){
int myInt = 7;
int myInt2 = 2;
System.out.println("myInt = "+myInt);
System.out.println("myInt2 = "+myInt2);
//Left Shift
System.out.println("myInt << myInt2 = "+ (myInt << myInt2));
//Signed Right Shift
System.out.println("myInt >> myInt2 = "+ (myInt >> myInt2));
//Unsigned Right Shift
System.out.println("myInt >>> myInt2 = "+ (myInt >>> myInt2));
//Changing the value of myInt to -7 and restoring myInt2 to 2
myInt = -7;
myInt2 = 2;
System.out.println("\nmyInt = "+myInt);
System.out.println("myInt2 = "+myInt2);
//Left Shift
System.out.println("myInt << myInt2 = "+ (myInt << myInt2));
//Signed Right Shift
System.out.println("myInt >> myInt2 = "+ (myInt >> myInt2));
//Unsigned Right Shift
System.out.println("myInt >>> myInt2 = "+ (myInt >>> myInt2));
}
}
myInt = 7
myInt2 = 2
myInt << myInt2 = 28
myInt >> myInt2 = 1
myInt >>> myInt2 = 1
myInt = -7
myInt2 = 2
myInt << myInt2 = -28
myInt >> myInt2 = -2
myInt >>> myInt2 = 1073741822
< , > , <= , >= , instanceof , == , !=
):The output of the relational operator is Boolean and self-explanatory. Let us just dive into the examples:
public class MyOperators{
public static void main(String []args){
int myInt = 7;
int myInt2 = 2;
System.out.println("myInt = "+myInt);
System.out.println("myInt2 = "+myInt2);
System.out.println("myInt < myInt2 = "+ (myInt < myInt2));
System.out.println("myInt > myInt2 = "+ (myInt > myInt2));
System.out.println("myInt <= myInt2 = "+ (myInt <= myInt2));
System.out.println("myInt >= myInt2 = "+ (myInt >= myInt2));
System.out.println("myInt == myInt2 = "+ (myInt == myInt2));
System.out.println("myInt != myInt2 = "+ (myInt != myInt2));
MyOperators myOp=new MyOperators();
System.out.println("myOp instanceof MyOperators = "+(myOp instanceof MyOperators));
}
}
myInt = 7 myInt2 = 2 myInt < myInt2 = false myInt > myInt2 = true myInt <= myInt2 = false myInt >= myInt2 = true myInt == myInt2 = false myInt != myInt2 = true myOp instanceof MyOperators = true
One thing to notice is that while checking the equality, we use == instead of =. = is used for assignment only. That is why == was introduced so that LHS and RHS can be checked for equality.
& , ^ , |
):One important thing to note is that both the bits are checked in all the bitwise operators irrespective of the bit value of the first operand being true(1)/false(0).
public class MyOperators{
public static void main(String []args){
int myInt = 7;
int myInt2 = 2;
System.out.println("myInt = "+myInt);
System.out.println("myInt2 = "+myInt2);
System.out.println("myInt & myInt2 = "+ (myInt & myInt2));
System.out.println("myInt ^ myInt2 = "+ (myInt ^ myInt2));
System.out.println("myInt | myInt2 = "+ (myInt | myInt2));
}
}
myInt = 7 myInt2 = 2 myInt & myInt2 = 2 myInt ^ myInt2 = 5 myInt | myInt2 = 7
&& , ||
):and
operator. It performs the and operator. It yields true(1) only if both the operands are true(1), else it returns false. The second operator is not evaluated in the first operand is false.
public class MyOperators{
public static void main(String []args){
boolean myBool = true;
boolean myBool2 = false;
System.out.println("myBool = "+myBool);
System.out.println("myBool2 = "+myBool2);
System.out.println("myBool && myBool2 = "+ (myBool && myBool2));
System.out.println("myBool || myBool2 = "+ (myBool || myBool2));
}
}
myBool = true myBool2 = false myBool && myBool2 = false myBool || myBool2 = true
?:
)The format of ternary is as follows:
Example:
public class MyOperators{
public static void main(String []args){
int myInt = 7;
int myInt2 = 2;
System.out.println("myInt = "+myInt);
System.out.println("myInt2 = "+myInt2);
System.out.println((myInt>myInt2)?"myInt is greater than myInt2":"myInt is less than myInt2");
System.out.println((myInt2>myInt)?"myInt2 is greater than myInt":"myInt2 is less than myInt");
}
}
myInt = 7
myInt2 = 2
myInt is greater than myInt2
myInt2 is less than myInt
Apart from so many operators mentioned above, there is another section of the operator which is used to manipulate the value of the left operand on the basis of the assignment operator and the right operand.
The use of <any of the arithmetic, shifting, bitwise operator> followed by = makes it an assignment operator.
Example: 7+=2 , 24/=3 , 7*=3 , 7<<=3
public class MyOperators{
public static void main(String []args){
int a=7;
int b=2;
int c=24;
int d=3;
System.out.println("7+=2 is "+(a+=b)); // a+=b mean a=a+b so now a = 9
System.out.println("24/=3 is "+(c/=d)); // c/=d mean c=c/d so now c = 8
System.out.println("9*=3 is "+(a*=d)); // a*=d mean a=a*d so now a = 27
System.out.println("27<<=3 is "+(a<<=d)); // a<<=d mean a=a<<d so now a = 216
}
}
7+=2 is 9
24/=3 is 8
9*=3 is 27
27<<=3 is 216
This was a detailed overview of Operators. We will discuss Java Decision-Making in the next section.