值“空”默认情况下在字符串中
我有这个类,
public class Model {
private String a;
private String b;
public synchronized String getA() {
return a;
}
public synchronized void setA(String a) {
this.a = a;
}
public synchronized String getB() {
return b;
}
public synchronized void setB(String b) {
this.b = b;
}
}
我尝试获取 a
的值,并且我知道通过 default 该变量初始化为 null
。但是,是否有可能,如果我调用 getA()
方法,此变量上会有字符串 "null"
(而不是 null
但是字符串
)?所以a.equals("null") == true
。
public static void main(String[] args) throws ParseException {
Model m = new Model();
String test = m.getA();
m.getA().equals("null");//No Exception occurs
事实上,
我评估字符串的代码是 Android 应用程序的一部分:
mAirline = (Airline) extras.getSerializable("airline");
@SuppressWarnings("unused")
String test = mAirline.getPhone(); //(1)
String test2 = mAirline.getHref(); //(2)
如果我在 (1) 中检查 mAirline,mAirline 的字段为 null,但在 (2) 中,其中一些字段为“null”,而我的方法为得到的是
public synchronized String getPhone() {
return phone;
}
I have this class
public class Model {
private String a;
private String b;
public synchronized String getA() {
return a;
}
public synchronized void setA(String a) {
this.a = a;
}
public synchronized String getB() {
return b;
}
public synchronized void setB(String b) {
this.b = b;
}
}
I try to get the value of a
, and I know that by default this variable is initialize to null
. But, is it possible that if I call the getA()
method, afterwards this variable has the String "null"
on it (not null
but the String
)? So a.equals("null") == true
.
public static void main(String[] args) throws ParseException {
Model m = new Model();
String test = m.getA();
m.getA().equals("null");//No Exception occurs
}
And in fact the code where I eval the String is part of an Android Application:
mAirline = (Airline) extras.getSerializable("airline");
@SuppressWarnings("unused")
String test = mAirline.getPhone(); //(1)
String test2 = mAirline.getHref(); //(2)
If I check mAirline in (1) mAirline has it fields in null, but in (2) has some of them to "null" And my method for get is
public synchronized String getPhone() {
return phone;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不,根据您向我们展示的代码,您不可能自动获取
String
"null"
。但请注意,某些方法会将将
null
转换为“null”
。最值得注意的示例是PrintWriter.println()
(如System.out.println()
) 和String.valueOf()
。这些行将分别打印
null
(即 4 个字符)和true
No, with the code you showed us, it's not possible that you automatically get the
String
"null"
.Note, however, that some methods will convert
null
to"null"
. The most notable examples arePrintWriter.println()
(as inSystem.out.println()
) andString.valueOf()
.These line will print
null
(i.e. the 4 characters) andtrue
respectively也许只是:
Maybe just:
为什么不直接检查字符串是否不为空呢?
您还可以更改 getter 方法,以便在字段未初始化时返回默认值:
Why don't you just check if the string is not null?
You can also alter the getter method, so it returns your default value if the field is not initialized:
如果参数为
null
,则方法String.valueOf()
返回"null"
;如果参数是与以下字符串不同的字符串,则返回参数本身:空
。但这有点神秘,很难理解你想要做什么。
直接检查
null
,更容易阅读:The method
String.valueOf()
returns"null"
if the argument isnull
or the argument itself if it is a String that is different fromnull
.but this is kind of cryptic, pretty hard to understand what you want to do.
Check for
null
directly, much easier to read:所以你的意思是,
a
的值是字符串null
,而不是值为null的指针?这种情况永远不会发生,除非您使用setA("null");
设置它。如果您希望
a
初始化为null,请编写构造函数:So you mean, the value of
a
is the stringnull
, rather than a pointer with value null? That well never happen, unless you set it like that usingsetA("null");
.If you want
a
to be initialized as null, write a constructor:这将为您解决问题。如果变量为空,您将返回“null”,如果不是,您将返回该值。
This will fix the problem for you. If the variable is null you will return "null" and if not you will return the value.