int i = 0;
long ll = 0L;
i = -1 - 2; // -3
i = (int) -1 - 2; // -3
ll = (long) - (int) -1 - 2;// -1
i = (int) +(long) -(int) -1 - 2;//-1
ll = (long) +(int) -1 - 2; // -3
In the above program at the line
ll = (long) - (int) -1-2;
ll
value is -1
. How does its value becomes -1
just by type casting? Shouldn't it be like 3 or -3?
i = (int) +(long) -(int) -1 - 2;
unary operator will be evaluated first then binary because of unary operator is higher precedence than binary operator.
so -(int) -1
will be evaluated first and it will become +1
then +(long)+1
will be +1
and after that (int)+1
will be evaluated so it will become +1
then +1-2
will be evaluated as -1
so you are getting -1
. same operation will be performed for other statements.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.