值“空”默认情况下在字符串中

发布于 2024-12-03 06:43:54 字数 1343 浏览 1 评论 0原文

我有这个类,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

究竟谁懂我的在乎 2024-12-10 06:43:54

不,根据您向我们展示的代码,您不可能自动获取 String "null"

但请注意,某些方法会将null转换为“null”。最值得注意的示例是 PrintWriter.println() (如 System.out.println()) 和 String.valueOf()

System.out.println(null);
System.out.println("null".equals(String.valueOf(null)));

这些行将分别打印 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 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

星星的軌跡 2024-12-10 06:43:54

也许只是:

private String a = "null";

Maybe just:

private String a = "null";
双马尾 2024-12-10 06:43:54

为什么不直接检查字符串是否不为空呢?

Model m = new Model();
String test = m.getA();
if(test==null) {
  //is null
} else {
  //is not null
}

您还可以更改 getter 方法,以便在字段未初始化时返回默认值:

public class Model {    
    private String a;

    public synchronized String getA() {
        //if a is null, return "null", else return a
        return a==null?"null":a;
    }

}

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;
    }

}
夜还是长夜 2024-12-10 06:43:54

如果参数为 null,则方法 String.valueOf() 返回 "null";如果参数是与以下字符串不同的字符串,则返回参数本身:

    Model m = new Model();
    String test = m.getA();
    if (String.valueOf(a).equals("null")) //No Exception occurs

但这有点神秘,很难理解你想要做什么。
直接检查 null,更容易阅读:

    Model m = new Model();
    String test = m.getA();
    if (a == null || a.equals("null")) //No Exception occurs

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
怪我鬧 2024-12-10 06:43:54

所以你的意思是,a的值是字符串null,而不是值为null的指针?这种情况永远不会发生,除非您使用 setA("null"); 设置它。

如果您希望a初始化为null,请编写构造函数:

public Model() {
    a = "null";
}

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";
}
小ぇ时光︴ 2024-12-10 06:43:54

这将为您解决问题。如果变量为空,您将返回“null”,如果不是,您将返回该值。

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;
    }   
}

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;
    }   
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文