为什么有两个结果不同 [英] Why have two result different

查看:78
本文介绍了为什么有两个结果不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么有两个结果不同?

why have two result different?

<pre>system.out.printtln ( 2 + 3 + " bc" )

system.out.printtln (  " bc" + 2 + 3)



结果

5bc

bc23



我的尝试:



为什么两个结果不同?


result
5bc
bc23

What I have tried:

why have two result different?

<pre>system.out.printtln ( 2 + 3 + " bc" )

system.out.printtln (  " bc" + 2 + 3)



结果

5bc

bc23


result
5bc
bc23

推荐答案

令人着迷的是,您没有问过如何添加数字和字符串而不是得到任何错误......

虽然算术加法没有顺序是完全正确的,但我看不出你期望成为你的加法运算的基数......

Java选择了做你的工作的方式 - 好的和坏的 - 并将加法的两边带到最低的公分母(意味着铸造类型都可以)...

由于加法是一个序列(你总是添加两个值),只有在必须完成时才会进行转换...

所以第一个版本是(( 2 + 3)+bc),第一部分是实际的算术加法,结果为5,只有第二个加法强制Java进行动态转换。

在第二个版本中,添加实际上是((bc+ 2)+ 3),这里第一个加法强制java强制转换为f所有添加内容都相同...
It is fascinating, that you didn't asked how it is possible that you can add numbers and strings and not getting any error...
While it is completely true that arithmetic addition has no order, I can't see on what base do you expect to be your addition arithmetic...
Java picked the way to do your work - for good and bad - and bring the two sides of the addition to the lowest common denominator (means cast to the type both can be)...
As addition is a sequence (you always add two values), the casting will be done only when it have to be done...
So the first version is ((2+3)+"bc"), and the first part is an actual arithmetic addition that results in 5, and only the second addition forces Java to do a dynamic cast.
In the second version the addition is actually (("bc" + 2) + 3), and here the very first addition forces java to cast and from there all the additions do the same...


检查Java参考,以便将字符串和字符串添加到数字的结果。
Check the Java reference for the results of adding numbers to strings and strings to numbers.


这篇关于为什么有两个结果不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆