gpt4 book ai didi

java - 将字符串设置为 ""还是保留为空?

转载 作者:搜寻专家 更新时间:2023-11-01 01:00:57 24 4
gpt4 key购买 nike

我有一些字符串变量,它们通过调用函数 getParameter() 获取值,其中一些变量可能是 null

稍后,我将使用 equals() 方法计算这个变量。如果所有字符串变量为 null,我是否应该将所有字符串变量设置为空字符串 ("") 以避免任何问题?

最佳答案

另一种方法是使用静态 util 方法进行比较。 Apache commons-lang StringUtils.equals(String,String)有明确定义的 null 行为是可能的。

// null safe compare
if (StringUtils.equals(variable,"hello")) {...}

// is "" or null
if (StringUtils.isEmpty(variable)) { ... }

// with static imports it's a bit nicer
if (isNotEmpty(var1) && isEmpty(var2)) { ... }

关于java - 将字符串设置为 ""还是保留为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1531302/

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