Java String.split是否可以返回null String []

编程入门 行业动态 更新时间:2024-10-26 06:38:25
本文介绍了Java String.split是否可以返回null String []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

split 是否可以返回null String [] ?我很好奇,因为我想在我的代码中尽可能保持防御,而不必进行不必要的检查。代码如下:

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:

String[] parts = myString.split("\\w");

我需要在使用部件 post splitting?

do I need to perform a null check before I use parts post splitting?

推荐答案

它永远不会返回null。如果您不确定,应该始终检查方法的javadoc。例如, String#split(String) 说

It never returns null. You should always check the javadoc of the method if you are not sure. For example String#split(String) says

此方法就好像通过调用双参数拆分方法

...和 String#split(String,int) 说:

...and String#split(String,int) says:

如果表达式不匹配输入的任何部分,那么结果数组只有一个元素,即这个字符串。

If the expression does not match any part of the input then the resulting array has just one element, namely this string.

从Javadoc你也可以找到可以发生什么样的异常,更重要的是为什么抛出这些异常。另外一个非常重要的事情是检查类是否是线程安全的。

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.

更多推荐

Java String.split是否可以返回null String []

本文发布于:2023-11-10 03:05:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574176.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:String   Java   null   split

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!