null和空字符串之间的区别

null和空字符串之间的区别

本文介绍了null和空字符串之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

空字符串( String s = null )和空字符串( String s =)?

What is the difference between a null string (String s = null) and an empty string (String s = "")?

这就是我所拥有的:

String s1 = ""; //print statement does not print any thing for s1 but s1.length()=0
String s2 = null;//print statement prints "null" for s2  but s2.length() gives exception

这是什么意思?

推荐答案

String s1 =; 表示空字符串已分配给 S1
在这种情况下, s1.length()。length()相同,这将按预期产生 0

String s1 = ""; means that the empty String is assigned to s1.In this case, s1.length() is the same as "".length(), which will yield 0 as expected.

String s2 = null; 表示( null )或没有任何价值被分配给 s2 。所以这个, s2.length() null.length()相同,这将产生一个 NullPointerException 因为你无法用Java调用 null 变量(指针,排序)上的方法。

String s2 = null; means that (null) or "no value at all" is assigned to s2. So this one, s2.length() is the same as null.length(), which will yield a NullPointerException as you can't call methods on null variables (pointers, sort of) in Java.

另外,一个点,声明

String s1;

实际上具有与以下相同的效果:

Actually has the same effect as:

String s1 = null;

然而

String s1 = "";

如上所述,是另一回事。

Is, as said, a different thing.

这篇关于null和空字符串之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 08:03