gpt4 book ai didi

java - 关于java中自动强制转换的问题?

转载 作者:太空宇宙 更新时间:2023-11-04 10:10:41 24 4
gpt4 key购买 nike

很多人推荐我看《Java - 完全引用》这本书。我实际上开始阅读它,并发现了这段文字:

"There are no automatic coercions or conversions of conflicting types as in some languages. The Java compiler checks all expressions and parameters to ensure that the types are compatible. Any type mismatches are errors that must be corrected before the"

这与以下内容不矛盾吗:System.out.println("字符串"+ a);

其中 a 是任何原始类型。这不是一个自动强制的隐式类型转换吗?

例如,字符串和整数不是兼容的类型。

注意:我的母语不是英语

最佳答案

要去的地方始终是 Java 语言规范。在本例中,我们讨论的是 15.18.1, String Concatenation Operator + :

If only one operand expression is of type String, then string conversion (§5.1.11) is performed on the other operand to produce a string at run time.

特别是,符号 + 根据上下文可以表示三种不同的含义:一元加、字符串连接或加法。

关于java - 关于java中自动强制转换的问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52375343/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com