英文:
how is the following expressions **2.0+"2"** evaluated step by step in Java? and how is it different from **2+"2"**?
问题
在Java中,数字会自动转换为字符串,因此 2+"2" 的结果为 22... 但是为什么 2.0+"2" 的结果却是 2.02...
这是我正在学习的地方:
https://www.coursera.org/learn/cs-programming-java/lecture/o5IxV/type-conversion
英文:
in java a number is automatically converted into a string ,hence 2+"2" evaluates to 22...
but why does 2.0+"2" evaluates to 2.02..
Here's where i m learning from
https://www.coursera.org/learn/cs-programming-java/lecture/o5IxV/type-conversion
答案1
得分: 3
当您将任何内容与String
连接在一起时,不是String
的内容必须转换为String
。所以2.0+"2"
变成了"2.0" + "2"
,而2+"2"
变成了"2"+"2"
。
英文:
When you concatenate anything with a String
, the thing that isn't a String
must be converted to one. So 2.0+"2"
is "2.0" + "2"
and 2+"2"
is "2"+"2"
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论