Is it possible for split
to return a null String[]
? I am curious as I want to try to be as defensive as possible in my code without having unnecessary checks. The code is as follows:
split可以返回null String []吗?我很好奇,因为我想在我的代码中尽可能保持防御,而不必进行不必要的检查。代码如下:
String[] parts = myString.split("\\w");
do I need to perform a null check before I use parts
post splitting?
在使用分割后的部分之前,是否需要执行空检查?
4 个解决方案
#1
45
It never returns null. You should always check the javadoc of the method if you are not sure. For example String#split(String)
says
它永远不会返回null。如果您不确定,应该始终检查方法的javadoc。比如String#split(String)说
This method works as if by invoking the two-argument
split
method此方法的工作方式就像调用双参数split方法一样
...and String#split(String,int)
says:
...和String#split(String,int)说:
If the expression does not match any part of the input then the resulting array has just one element, namely this string.
如果表达式与输入的任何部分都不匹配,那么结果数组只有一个元素,即该字符串。
From Javadoc you can also find out what kind of exceptions can happen and more importantly why were those exceptions thrown. Also one very important thing to check is if the classes are thread safe or not.
从Javadoc您还可以找出可能发生的异常类型,更重要的是为什么抛出这些异常。另外一个非常重要的事情是检查类是否是线程安全的。
#2
7
I recommend you download the Java source code for those times when the API is unclear. All the answers here just tell you the answer but you should really see for yourself. You can download the Java source code from here (look near the bottom of the page).
我建议您在API不清楚时下载Java源代码。这里的所有答案都只是告诉你答案,但你应该亲自看看。您可以从此处下载Java源代码(查看页面底部附近)。
By following the source code you'll end up at String[] Pattern.split(CharSequence input, int limit)
. The return value comes from a non-null ArrayList
on which toArray
is called. toArray
does not return null: in the event of an empty list you'll get an empty array back.
通过遵循源代码,您将最终得到String [] Pattern.split(CharSequence输入,int limit)。返回值来自调用toArray的非null ArrayList。 toArray不返回null:如果是空列表,你将得到一个空数组。
So in the end, no, you don't have to check for null.
所以最后,不,你不必检查null。
#3
1
No, you don't need to check for null on the parts.
不,您不需要检查零件上的空值。
#4
1
Split method calls the Patter.split method which does this in the beginning of the method:
Split方法调用Patter.split方法,该方法在方法的开头执行此操作:
ArrayList<String> matchList = new ArrayList<String>();
ArrayList
And at the end does matchList.toArray() to return an Array.
最后,matchList.toArray()返回一个数组。
So no need to test of nulls.
所以不需要测试空值。
#1
45
It never returns null. You should always check the javadoc of the method if you are not sure. For example String#split(String)
says
它永远不会返回null。如果您不确定,应该始终检查方法的javadoc。比如String#split(String)说
This method works as if by invoking the two-argument
split
method此方法的工作方式就像调用双参数split方法一样
...and String#split(String,int)
says:
...和String#split(String,int)说:
If the expression does not match any part of the input then the resulting array has just one element, namely this string.
如果表达式与输入的任何部分都不匹配,那么结果数组只有一个元素,即该字符串。
From Javadoc you can also find out what kind of exceptions can happen and more importantly why were those exceptions thrown. Also one very important thing to check is if the classes are thread safe or not.
从Javadoc您还可以找出可能发生的异常类型,更重要的是为什么抛出这些异常。另外一个非常重要的事情是检查类是否是线程安全的。
#2
7
I recommend you download the Java source code for those times when the API is unclear. All the answers here just tell you the answer but you should really see for yourself. You can download the Java source code from here (look near the bottom of the page).
我建议您在API不清楚时下载Java源代码。这里的所有答案都只是告诉你答案,但你应该亲自看看。您可以从此处下载Java源代码(查看页面底部附近)。
By following the source code you'll end up at String[] Pattern.split(CharSequence input, int limit)
. The return value comes from a non-null ArrayList
on which toArray
is called. toArray
does not return null: in the event of an empty list you'll get an empty array back.
通过遵循源代码,您将最终得到String [] Pattern.split(CharSequence输入,int limit)。返回值来自调用toArray的非null ArrayList。 toArray不返回null:如果是空列表,你将得到一个空数组。
So in the end, no, you don't have to check for null.
所以最后,不,你不必检查null。
#3
1
No, you don't need to check for null on the parts.
不,您不需要检查零件上的空值。
#4
1
Split method calls the Patter.split method which does this in the beginning of the method:
Split方法调用Patter.split方法,该方法在方法的开头执行此操作:
ArrayList<String> matchList = new ArrayList<String>();
ArrayList
And at the end does matchList.toArray() to return an Array.
最后,matchList.toArray()返回一个数组。
So no need to test of nulls.
所以不需要测试空值。