Value "null" by default in String
I have开发者_C百科 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;
}
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 are PrintWriter.println()
(as in System.out.println()
) and String.valueOf()
.
System.out.println(null);
System.out.println("null".equals(String.valueOf(null)));
These line will print null
(i.e. the 4 characters) and true
respectively
Maybe just:
private String a = "null";
Why don't you just check if the string is not null?
Model m = new Model();
String test = m.getA();
if(test==null) {
//is null
} else {
//is not null
}
You can also alter the getter method, so it returns your default value if the field is not initialized:
public class Model {
private String a;
public synchronized String getA() {
//if a is null, return "null", else return a
return a==null?"null":a;
}
}
The method String.valueOf()
returns "null"
if the argument is null
or the argument itself if it is a String that is different from null
.
Model m = new Model();
String test = m.getA();
if (String.valueOf(a).equals("null")) //No Exception occurs
but this is kind of cryptic, pretty hard to understand what you want to do.
Check for null
directly, much easier to read:
Model m = new Model();
String test = m.getA();
if (a == null || a.equals("null")) //No Exception occurs
So you mean, the value of a
is the string null
, rather than a pointer with value null? That well never happen, unless you set it like that using setA("null");
.
If you want a
to be initialized as null, write a constructor:
public Model() {
a = "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.
public class Model {
private String a;
private String b;
public synchronized String getA() {
return (if (a ==null) ? "null" : a);
}
public synchronized void setA(String a) {
this.a = a;
}
public synchronized String getB() {
return (if (b ==null) ? "null" : b);
}
public synchronized void setB(String b) {
this.b = b;
}
}
精彩评论