当前位置:网站首页>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);
}
}

边栏推荐
- Dabao and Erbao
- Kotlin's list, map, set and other collection classes do not specify types
- Implementation of jump connection of RESNET (pytorch)
- Fuzzy query of SQL
- Code or script to speed up the video playback of video websites
- 12. Priority queue and inert queue
- openFeign异步调用问题
- It won't last for 65 days. It only appears once
- Whole house WiFi solution: mesh router networking and ac+ap
- 从淘宝,天猫,1688,微店,京东,苏宁,淘特等其他平台一键复制商品到拼多多平台(批量上传宝贝详情接口教程)
猜你喜欢

不会就坚持66天吧 权重生成随机数

6. Pytest generates an allure Report
![[hands on deep learning] environment configuration (detailed records, starting from the installation of VMware virtual machine)](/img/fe/8c707c30c734de7bb76ea68134842c.png)
[hands on deep learning] environment configuration (detailed records, starting from the installation of VMware virtual machine)

编译与链接

Class starts! See how smardaten decomposes complex business scenarios

12.优先级队列和惰性队列

Copy products with one click from Taobao, tmall, 1688, wechat, jd.com, Suning, taote and other platforms to pinduoduo platform (batch upload baby details Interface tutorial)

MySQL - 深入解析MySQL索引数据结构

Not for 61 days. The shortest word code

不会就坚持67天吧 平方根
随机推荐
不会就坚持69天吧 合并区间
不会就坚持66天吧 权重生成随机数
Copy products with one click from Taobao, tmall, 1688, wechat, jd.com, Suning, taote and other platforms to pinduoduo platform (batch upload baby details Interface tutorial)
Code or script to speed up the video playback of video websites
MySQL - 深入解析MySQL索引数据结构
How to solve the problem of store ranking?
openFeign异步调用问题
Exception handling: pyemd or pyemd not found
Why are there so many unknowns when opengauss starts?
Target detection learning process
String, array, generalized table (detailed)
C language: summary of consortium knowledge points
Installation and use of stm32cubemx (5.3.0)
12. Priority queue and inert queue
Dabao and Erbao
settings.xml
C language: typedef knowledge points summary
No, just stick to it for 59 days
14.haproxy+keepalived负载均衡和高可用
恒星科通邀您“湘”约第24届中国高速公路信息化大会暨技术产品展示会