Java中x=x+y与x+=y的区别,体现强制类型转换
来源:互联网 发布:靠谱的网络整合营销 编辑:程序博客网 时间:2024/04/28 15:49
int x+=y其实是x=(int)(x+y)
直接见下面的代码示例:
public class testCasting { public static void main(String arg[]) {int a = 10;int b = 20;double d = 12.3;char c = 'c';String s = "abc";//下面输出:The result of a = a + b is:30System.out.println("The result of a = a + b is:" + (a=a+b)); //下面输出:The result of a += b is:50System.out.println("The result of a += b is:" + (a+=b)); //下面输出:The result of a += d is:62(这里体现了强制类型转换为int)System.out.println("The result of a += d is:" + (a+=d)); //下面输出:The result of a = a + d is:74(这里需要使用(int)d来进行强制转换,不然编译不过)System.out.println("The result of a = a + d is:"+ (a=a+(int)d));//下面输出:The result of a = a + d is:86.3(这里体现表达式中最大的数据类型决定最终结果的数据类型)System.out.println("The result of a = a + d is:"+ (a+d)); //下面输出:The result of a = a + c is:173(这里体现了只要类型比int,那么在运算之前,这些值会自动转换为int)System.out.println("The result of a = a + c is:" + (a=a+c));//下面输出:The result of a += c is:272System.out.println("The result of a += c is:" + (a+=c)); //下面输出:The result of c += a is:ų(这里体现了c+=a其实是c=(char)(c+a)的实质,多了一层强制转换)System.out.println("The result of c += a is:" + (c+=a)); //下面输出:The result of s = s + a is:abc272(下面两个涉及String的操作,体现了String的特性,表达式中出现String开头的,之后的都会转换为String进行连接)System.out.println("The result of s = s + a is:" + (s=s+a));//下面输出:The result of s += a is:abc272272System.out.println("The result of s += a is:" + (s+=a));//下面输出:The result of s = s + c is:abc272272ųSystem.out.println("The result of s = s + c is:" + (s=s+c)); }}
- Java中x=x+y与x+=y的区别,体现强制类型转换
- x+=y与x=x+y的区别
- x+=y与x=x+y的区别
- Java中x+=y和x=x+y两种实现的区别
- Java基础:X+=Y和X=X+Y的区别
- 怎么更容易记住y = x++ 与 y = ++x的区别
- 比较表达式 x<y 与x-y<0的区别
- atan(y/x)与atan2(y,x)的区别
- 解方程: x+y=x|y
- 对JS中x==y类型转换方向的探究
- x=min(x, y)
- 计算函数F(x,y,z)=(x+y)/(x-y)+(z+y)/(z-y)的值
- y=mysqrt(x)
- 求28 * x + 15 * y = 95108的所有x与y的整数值个数
- Android scrollBy(x,y)和scrollTo(x,y)的区别
- y=x++ + ++x(int x=0)的结果?
- train_test_split(X, y, stratify=y)
- 新手喜闻乐见的x++,y++,++x,++y
- QT中DLL的生成和调用(查了些资料在同事的帮助下完成)
- 部署到AIX系统乱码问题
- misc.properties文件的一些配置的实例
- InnoDB引擎表的主键选型
- 重大疑难故障分析日志(已经解决)
- Java中x=x+y与x+=y的区别,体现强制类型转换
- HeadFirst学习笔记2:抽象工厂模式
- 我的python学习之路----转换位串到utf-8字符串
- 商务局 存储过程
- 03-05 创建和编辑AutoCAD实体(五) 使用图层、颜色和线型(2)使用颜色
- 下面程序的输出结果是多少?
- 自然语言理解双重路径
- Linux操作系统常用命令
- delphi 用线程监视某程序