Java中的++和--
一、认识++和--
-
++。
当++在前面的时候,先自加1,后进行赋值操作;当++在后面的时候,先进行赋值操作,再自加1。 -
--。
当--在前面的时候,先自减1,后进行赋值操作;当--在后面的时候,先进行赋值操作,再自减1.
/*
++:自加1
当++在前的时候,先自加1,再做赋值操作,当++在后的时候,先做赋值操作,再做自加1
--:自减1
当--在前的时候,先自减1,再做赋值操作,当--在后的时候,先做赋值操作,再做自减1
*/
public class DataTypeDemo8 {
public static void main(String[] args) {
// int a = 10;
// int c = a--;
// System.out.println(a); //9
// System.out.println(c); //10
//
// int a1 = 10;
// int c1 = --a1;
// System.out.println(a1); //9
// System.out.println(c1); //9
int a = 10;
int c = a++;
System.out.println(a); //11
System.out.println(c); //10
int a1 = 10;
int c1 = ++a1;
System.out.println(a1); //11
System.out.println(c1); //11
}
}
Question1.
/*
1:基本小题目
int a = 10;
int b = 10;
int c = 10;
a = b++;
c = --a;
b = ++a;
a = c--;
请分别计算出a,b,c的值
*/
public class OptArithmeticDemo2 {
public static void main(String[] args) {
int a = 10;
int b = 10;
int c = 10;
a = b++; // a=10 ,b=11 ,c=10
c = --a; // a=9 ,b=11 ,c=9
b = ++a; // a=10 ,b=10 ,c=9
a = c--; // a=9 ,b=10 ,c=8
System.out.println(a); // 9
System.out.println(b); // 10
System.out.println(c); // 8
}
}
Question2.
/*
int a = 4;
int b = (a++)+(++a)+(a*10);
*/
public class OptArithmeticDemo3 {
public static void main(String[] args) {
int a = 4;
int b = (a++)+(++a)+(a*10);
// 4 6 60
// a=5 a=6 a=6
System.out.println(a);//6
System.out.println(b);//70
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决