当前位置:网站首页>i++与++i详解
i++与++i详解
2022-07-29 04:23:00 【赟文武】
i++与++i详解
i++与++i不与赋值号(=)连接时效果等同于i=i+1
示例如下:
//++i
public class demo
{
public static void main(String[] args) {
int i=0;
++i;
System.out.println(i);
}
}
//i++
public class demo
{
public static void main(String[] args) {
int i=0;
i++;
System.out.println(i);
}
}
两个代码块输出结果均为1
i++与赋值号(=)同时出现时表现先取i的值进行相关运算然后再加一
public class demo{
public static void main(String[] args){
int q;
int i=0;
q=++i;
System.out.println(q);
}
}

++i与赋值号(=)同时出现时先加1再取值
public class demo{
public static void main(String[] args){
int q;
int i=0;
q=++i;
System.out.println(q);
}
}

现在我们将i++与++i与赋值号(=)放到一块讨论其顺序
public class demo
{
public static void main(String[] args) {
int q;
int i=0;
q=(++i)+(i++);
System.out.println(q);
System.out.println(i);
}
}

我们把q=(++i)+(i++)拎出来看
在这步之前我们的q未赋值,i的值为0,
运行到此处时()的优先级最高先执行括号里的东西第一个括号表示++i将i+1赋给i,此时i变成了1参与括号外+法运算时为1,再看第二个括号i++表示先将i值赋给i再给i+1此时i为2,但需要注意一点,i++是赋值后再加所以右括号参加两个括号外的加法运算时的值为(当前值-1)即1。所以q,i的值最后均会为2
现在我们提升一下难度,加3个
public class demo
{
public static void main(String[] args) {
int q;
int i=0;
q=(++i)+(i++)+(++i);
System.out.println(q);
System.out.println(i);
}
}

此处单拎q=(++i)+(i++)+(++i)该语句来看
该语句之上q无值,i值为0
到该句时运行括号里从左到右:
- 第一个括号里0+1,i的值变成了1,1参与外部运算
- 第二个括号里1+1,i的值变成了2,1参与外部运算
- 第三个括号里2+1,i的值变成了3,3参与外部运算
q被赋值为1+1+3为5,i最终值为3
上述语句等同于如下语句:
public class demo
{
public static void main(String[] args) {
int q;
int i=0;
int a=++i;
int b=i++;
int c=++i;
q=a+b+c;
System.out.println(q);
System.out.println(i);
}
}

边栏推荐
猜你喜欢
![[paper translation] vectornet: encoding HD maps and agent dynamics from vectorized representation](/img/4b/150689d5e4809ae66a4297915ecd0c.png)
[paper translation] vectornet: encoding HD maps and agent dynamics from vectorized representation

14. Haproxy+kept load balancing and high availability

Don't insist on 66 days. Weight generates random numbers

visio画网格

不会就坚持65天吧 只出现一次的数字

编译与链接

11.备份交换机

你真的会写Restful API吗?

Don't stick to it for 68 days. Baboons eat bananas

C language force buckle question 61 of the rotating list. Double ended queue and construction of circular linked list
随机推荐
编译与链接
How to set the SQL execution timeout for flick SQL
Not for 58 days. Implement prefix tree
Definition and implementation of stack and queue (detailed)
RMAN do not mark expired backups
Locker 2022.1.1
Use of torch.optim optimizer in pytorch
小程序:区域滚动、下拉刷新、上拉加载更多
[k210 stepping pit] pytorch model is converted to kmodel and used on dock. (ultimately not achieved)
The data source is SQL server. I want to configure the incremental data of the last two days of the date field updatedate to add
Applet: Area scrolling, pull-down refresh, pull-up load more
Multi rotor six axis hardware selection
Two forms of softmax cross entropy + numpy implementation
MySQL - 聚簇索引和辅助索引
Deploy Jenkins using containers
leetcode 686.重复叠加字符串 KMP方法(C语言实现)
Interview notes of a company
Target detection learning process
TypeError: Cannot read properties of undefined (reading ‘then‘)
[material delivery UAV] record (ROS + Px4 + yolov5 + esp8266 + steering gear)