how is the following expressions **2.0+"2"** evaluated step by step in Java? and how is it different from **2+"2"**?

huangapple go评论65阅读模式
英文:

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".

huangapple
  • 本文由 发表于 2020年8月30日 01:56:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63650136.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定