这里需要空检查吗?
Is null check required here?
我在我的项目中遇到了这行代码:
String statusRecoTime = sdf.format(cal.getTime());
然后有一个像
这样的空检查
if (statusRecoTime != null) {
//do something
}
我认为 statusRecoTime
永远不会为 null 并且不需要此检查,因为它已分配了一个对象。
请问我的理解是否正确?
不,这里不需要任何空值检查,因为 sdf 将无法格式化时间,它将抛出错误而不是空值。虽然如果您在 sdf .
中定义了正确的格式,但在格式化日历异常的时间时也不会出现
假设sdf
是一个SimpleDateFormat
实例,format
永远不会returnnull
。 null
检查完全没有必要。
这里的各种答案都关心该代码是否会抛出异常。唯一的情况是 sdf
或 cal
是 null
。但是假设两者都是非 null
并且 cal
是 Calendar
,该代码不会抛出异常。
来自SimpleDateFormat docs,方法returns a NullPointerException
如果传递的日期为空:
SimpleDateFormat.format()
Formats the given Date
into a date/time string and appends the result
to the given StringBuffer
.
Specified by:
format
in class DateFormat
Parameters:
date
- the date-time value to be formatted into a date-time string.
toAppendTo
- where the new date-time text is to be appended.
pos
- the formatting position. On input: an alignment field, if desired. On output: the offsets of the alignment field.
Returns: the formatted date-time string.
Throws:
NullPointerException
- if the given date is null.
没有必要检查 statusRecoTime != null
,因为如果 null
.
就会出现异常
我假设 sdf
是一个 SimpleDateFormat
实例。
首先要检查的是文档,正如 Mistalis 在 中所做的那样。单参数format
方法在超类DateFormat
中声明,文档说
Returns:
the formatted time string.
这应该足够了。没有说可以returnnull
,所以应该不用查了。我们可以到此为止。
如果您想更加确定,我们可以检查您正在使用的 Java 版本的源代码。在 Java 8 中(我希望在所有版本中)format(Date)
调用一个三参数 format
方法,返回一个 StringBuffer
并调用它的 toString()
方法。 StringBuffer.toString()
构成 new String()
。 new
保证永远不会 return null
。所以现在我们可以确定了。
例外:邪恶的人可能会编写 SimpleDateFormat
的子类,其中 format(Date)
可能 return null
与文档冲突。您的 sdf
变量可能包含此类邪恶子类的实例。你可以获得 null
。如果您从代码中知道 sdf
始终是 SimpleDateFormat
而不是某些本土或第三方子类,我们可以排除这种可能性。
我在我的项目中遇到了这行代码:
String statusRecoTime = sdf.format(cal.getTime());
然后有一个像
这样的空检查if (statusRecoTime != null) {
//do something
}
我认为 statusRecoTime
永远不会为 null 并且不需要此检查,因为它已分配了一个对象。
请问我的理解是否正确?
不,这里不需要任何空值检查,因为 sdf 将无法格式化时间,它将抛出错误而不是空值。虽然如果您在 sdf .
中定义了正确的格式,但在格式化日历异常的时间时也不会出现假设sdf
是一个SimpleDateFormat
实例,format
永远不会returnnull
。 null
检查完全没有必要。
这里的各种答案都关心该代码是否会抛出异常。唯一的情况是 sdf
或 cal
是 null
。但是假设两者都是非 null
并且 cal
是 Calendar
,该代码不会抛出异常。
来自SimpleDateFormat docs,方法returns a NullPointerException
如果传递的日期为空:
SimpleDateFormat.format()
Formats the given
Date
into a date/time string and appends the result to the givenStringBuffer
.Specified by:
format
in classDateFormat
Parameters:date
- the date-time value to be formatted into a date-time string.
toAppendTo
- where the new date-time text is to be appended.
pos
- the formatting position. On input: an alignment field, if desired. On output: the offsets of the alignment field.
Returns: the formatted date-time string.
Throws:NullPointerException
- if the given date is null.
没有必要检查 statusRecoTime != null
,因为如果 null
.
我假设 sdf
是一个 SimpleDateFormat
实例。
首先要检查的是文档,正如 Mistalis 在 format
方法在超类DateFormat
中声明,文档说
Returns:
the formatted time string.
这应该足够了。没有说可以returnnull
,所以应该不用查了。我们可以到此为止。
如果您想更加确定,我们可以检查您正在使用的 Java 版本的源代码。在 Java 8 中(我希望在所有版本中)format(Date)
调用一个三参数 format
方法,返回一个 StringBuffer
并调用它的 toString()
方法。 StringBuffer.toString()
构成 new String()
。 new
保证永远不会 return null
。所以现在我们可以确定了。
例外:邪恶的人可能会编写 SimpleDateFormat
的子类,其中 format(Date)
可能 return null
与文档冲突。您的 sdf
变量可能包含此类邪恶子类的实例。你可以获得 null
。如果您从代码中知道 sdf
始终是 SimpleDateFormat
而不是某些本土或第三方子类,我们可以排除这种可能性。