为什么“abc” +空结果 abcnull
为什么 "abc" + null
结果 abcnull
String s1 = "abc";
String s2 = null;
String s3 = s1+ s2;
System.out.println(s3);
结果:abcnull
why "abc" + null
results abcnull
String s1 = "abc";
String s2 = null;
String s3 = s1+ s2;
System.out.println(s3);
Result: abcnull
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
因为Java会创建一个 StringBuilder 将
s1
或"abc",
附加到s2
或null
。根据 StringBuilder.append(String) 的规范 -
所以它变成与
"abc" + "null"
相同让我们看一下你的代码示例(我将它放在一个方法中):
如果我们看一下字节码(通过调用 javap -c 获得):
Java 创建一个
StringBuilder
并将值附加为s1
的String
s > 和s2
。因此,如果您期待 NullPointerException,那么您必须小心,因为 Java 中的串联会解决这个问题。
旁注:正如 Jon Skeet 所指出的,这最终只是实现细节 - - Java 规范保证 null 在转换为字符串时会变成“null”。然而,这个字节码至少显示了幕后实际发生的事情。
Because Java will create a StringBuilder to append
s1
, or"abc",
tos2
, ornull
.According to the spec for StringBuilder.append(String)--
So it turns into the same as
"abc" + "null"
Let's take your code example (I placed it inside a method):
If we take a gander at the bytecode (gotten by invoking javap -c):
Java creates a
StringBuilder
and appends the values, asString
s, ofs1
ands2
.So you have to be careful then, if you were expecting a NullPointerException, because concatenation in Java will skirt the issue.
Side Note: As pointed out by Jon Skeet, this is ultimately just implementation details -- it's the Java spec that guarantees that null, when converted to a String, turns into "null". However, this bytecode at least shows what is actually happening behind the scenes.
这正是语言规范所保证的值。来自 JLS 第 15.8.1.1 节 ,其中讨论了用于字符串连接的字符串转换:
(您可能有兴趣听说 .NET 采用不同的方法,将空引用转换为空字符串。每种方法都有其优点和缺点 - 我发现 Java 方法对于创建用于调试的字符串更有用,但 .NET在构建要显示给用户或保存到数据文件等的字符串时,该方法更有用)
That's exactly the value that's guaranteed by the language specification. From the JLS section 15.8.1.1, which talks about the string conversions used for string concatenation:
(You may be interested to hear that .NET takes a different approach, converting a null reference to an empty string. Each approach has its pros and cons - I find the Java approach more useful for creating a string for debugging, but the .NET approach more useful when building a string to be displayed to a user or saved to a data file etc.)
这就是字符串连接的工作原理。
如果您需要避免连接单词“null”,您可以这样做:
That's how string concatenation works.
If you need to avoid concatenating the word "null" you can do:
我不了解java,但从它的外观来看,似乎您正在将
s2
类型转换为 String 对象,然后将null
转换为"null “
。如果我错了,请纠正我。i don't know java, but from the looks of it, it seems like you are type casting
s2
as a String object which then turnsnull
into"null"
. Please correct me if i'm wrong.理解的
编译器是这样
在你的例子中
compiler understands it as
so
In your example
Java中的字符串连接执行自动转换。空字符串将转换为字符串“null”。如需参考,请参阅此页面:
为了避免这种情况,您应该自己检查一个字符串是否为空。
String Concatenation in Java executes automatic conversion. The null string will be converted to the string 'null'. For reference, see f.e. this page:
To avoid this, you should check if one string is null yourself.
这就是 Java 连接的工作原理。
它将 null 对象视为“null”。
请参阅此
This is how Java Concatenation works.
It treats null objects as "null".
Please see this link
这是因为任何你用字符串添加的东西都会默认将其转换为字符串。
即使您尝试将数字添加到字符串中,它也会被转换为字符串。
例子:
“abc”+123=“abc123”
有一种名为 concat 的语法,它只会将字符串添加到字符串,而不添加任何其他内容。
例子 :
如果你输入
"abc".concat(null)
然后它会显示
java.lang.NullPointerException
在 java.lang.String.concat(来源未知)
但如果你输入
"abc".concat("null")
输出将为“abcnull”。
it is because any thing u add it with a string will by default convert it into a string.
and even if you try to add a number to a string it will be converted into string.
example:
"abc"+123="abc123"
there is a syntax called concat which will add only string to string not any thing else.
example :
if u type
"abc".concat(null)
then it will show
java.lang.NullPointerException
at java.lang.String.concat(Unknown Source)
but if u type
"abc".concat("null")
the output will be "abcnull".
让我们编译您的代码并查看字节码:
那么
"null"
如何最终出现在结果中?嗯,这在StringBuilder.append(String)
的文档中进行了描述:正如 Jon Skeet 指出的那样,这完全符合 Java 语言规范。
Let's compile your code and look at the bytecode:
So how does
"null"
end up in the result? Well, this is described in the documentation ofStringBuilder.append(String)
:As Jon Skeet points out, this is all in accordance with the Java Language Specification.