Unary operators
|
Precedence |
Operator |
Type |
Associativity |
|
13 |
+ + |
Unary pre- increment |
Right to Left |
|
-- |
Unary pre-
decrement |
||
|
+ |
Unary plus |
||
|
- |
Unary minus |
||
|
! |
Unary logic negation |
||
|
~ |
Unary bitwise
complement |
||
|
(type) |
Unary type cast |
||
|
14 |
+ + |
Unary post-
increment |
Right to Left |
|
-- |
Unary post- decrement |
||
|
15 |
( ) |
Parentheses |
Left to Right |
|
[ ] |
Array subscript |
||
|
. |
Member
selection |
Let’s see first about the operators with precedence 13. Unary pre-increment operators are used before a variable. In pre increment operators, the value of first increased and then the increased value is assigned to the variable. Similarly, in pre decrement operator, the input variable is first decreased then assigned to the new variable. This pre increment / decrement operators are used to perform general addition and subtraction operation. The following code can be used to explain this operator functionality.
class
operator{
public static void main(String
args[]){
//Assign variable 'a' as 11 and
defining b and c
short a=11, b,c;
//a=11 is assigned and hence it's
printed
System.out.println(a);
//The pre-increment operator
increments the value of 'b' as 12
b=++a;
//Here b=12 and c is b-1 and
hence the output is 11
System.out.println(b);
c=--b;
System.out.println(c);
}
}

Unary
plus, Unary minus and Logic negation operators
Unary plus operator prints the input as its output. If ‘11’ is given as input, the output is also 11. In case of unary minus operator, the negative value of the given input is printed. If ‘-11’ is given as input, the unary minus operator printed out ‘11’ in the output since – (-11) is 11. Logic negation (!) operator is applied only for Boolean inputs (True or False). Whenever ‘true’ is given is input, ‘false’ is printed in the output and vice versa. The following code implements unary plus, unary minus and logic negation operations in Java.
class operator{
public static void main(String args[]){
//Let's assume 2 variables
int x=29,z=179;
//Performing unary plus operations
int a=+x;
int b=+z;
//Performing unary minus operations
int c=-x;
int d=-z;
//Printing Boolean outputs using Logical negation operator
//a is not greater than b. So the output is False.
//Since negation operator is used, the output becomes true
System.out.println("!(a>b)" + !(a>b));
System.out.println("!(c>d)" + !(c>d));
}
}
Unary bitwise complement
Unary bitwise complement operator is used to invert
the input bits like 0 to 1 and vice versa.
· Let’s
consider !11 and the following steps occur sequentially
· First,
the binary equivalent of 11 is stored in 32-bit int data type like: 00000000
00000000 00000000 00001011
·
The
complement of each bit is taken: 11111111 11111111 11111111 11110100
· Since
the Most Significant Bit (MSB), the leftmost bit is 1, the output is negative
·
This
output is stored in 2’s complement form:
2’s complement= 1’s complement +1
·
1’s
complement is 00000000 00000000 00000000 00001011
1’s complement +1 is 00000000 00000000 00000000
00001100 whose decimal equivalent is 12 and hence the output is -12
Type cast operator
In Java, there is a lot of primitive data type
operators and if you want to know more about these primitive data types, you
can CHECK HERE. In the middle of the program,
you can easily change a variable from one data type to another and in such
cases, these type cast operators are used. There are two types of casting
namely narrowing and widening casting. Narrowing casting has to be done
manually and is implemented whenever user wants to convert larger data type
into smaller data type. Widening casting is the reverse process of narrowing
casting and this is done automatically by the compiler.
Java program using
bitwise complement and type cast operator
class
operator{
public static void main(String
args[]){
//Declaring x as integer
int x=11;
//bitwise complement operator
System.out.println("The
bitwise complement output of x is " + ~x);
//Type cast- Widening casting
double y=x; //Automatically
assigning an int to double
System.out.println("Widening
casting of x is " + y);
//Double data type gives out
floating point values
//Narrowing casting
short z= (short) x; //Manually
assigning an int to short
System.out.println("Narrowing
casting of x is " +z);
}
}
Output
Finally, the operators with the highest precedence
includes parentheses, Array subscript and member selection.
To learn more about Java operators, you can check these useful links:




0 Comments