java 中的空字符和空字符都相等
Both null and empty char are equal in java
我在 null
或空 char
登记 Java 时有疑问。两者都必须以任何方式检查。
例如在数据库中 Char
的变量长度是 1 。它也将允许 null
。所以如果你有空字符,它是否意味着 null
?或者我们必须检查
if(someObject.getSomeCharValue()=='' && someObject.getSomeCharValue()==null) {
//true
}
else{
//dont compile
}
空字符不为空。它们不仅具有任何价值。
是的。如果您的列允许空值,您应该检查两者。此外,您的代码可能会给您 NullPointerException
,因为您首先检查空字符,然后检查 null。最好在下面使用
if(someObject.getSomeCharValue()==null || someObject.getSomeCharValue()=='' )
if(someObject.getSomeCharValue()=='' && someObject.getSomeCharValue()==null)
someObject.getSomeCharValue()==''
和someObject.getSomeCharValue()==null
都是编译错误。
如果数据库中没有值,则表示它是 null 但不是空字符。
Java.
中没有所谓的空字符 (''
)
但是有空字符串 (""
).
char 在 ''
中没有值。在 char 中,null character (or empty char) 是 [=14=]
或 \u0000
。您无法使用 ''
或 null
.
检查
例如,如果您这样声明一个字符:
char c = '';//compilation error here
或
char c = null;//compilation error here
所以如果你想检查一个char是否为null,那么你需要使用下面的代码:
char c = '[=12=]';
if (c == '[=12=]') System.out.print("char is null");//if(c == '\u0000') also can be possible
else System.out.print("char is not null");
我在 null
或空 char
登记 Java 时有疑问。两者都必须以任何方式检查。
例如在数据库中 Char
的变量长度是 1 。它也将允许 null
。所以如果你有空字符,它是否意味着 null
?或者我们必须检查
if(someObject.getSomeCharValue()=='' && someObject.getSomeCharValue()==null) {
//true
}
else{
//dont compile
}
空字符不为空。它们不仅具有任何价值。
是的。如果您的列允许空值,您应该检查两者。此外,您的代码可能会给您 NullPointerException
,因为您首先检查空字符,然后检查 null。最好在下面使用
if(someObject.getSomeCharValue()==null || someObject.getSomeCharValue()=='' )
if(someObject.getSomeCharValue()=='' && someObject.getSomeCharValue()==null)
someObject.getSomeCharValue()==''
和someObject.getSomeCharValue()==null
都是编译错误。
如果数据库中没有值,则表示它是 null 但不是空字符。
Java.
中没有所谓的空字符 (''
)
但是有空字符串 (""
).
char 在 ''
中没有值。在 char 中,null character (or empty char) 是 [=14=]
或 \u0000
。您无法使用 ''
或 null
.
例如,如果您这样声明一个字符:
char c = '';//compilation error here
或
char c = null;//compilation error here
所以如果你想检查一个char是否为null,那么你需要使用下面的代码:
char c = '[=12=]';
if (c == '[=12=]') System.out.print("char is null");//if(c == '\u0000') also can be possible
else System.out.print("char is not null");