简体   繁体   English

Java Double初始化为0.0

[英]Java Double getting initialized to 0.0

I have a bean where I have a field "CustAmount "which is double, I tried testing the bean and i dont seem to understand this: When i run on my local machine it gets initialized to 0.0 when instantiated. 我有一个Bean,其中有一个字段“ CustAmount”,该字段为double,我尝试测试该Bean,但我似乎不明白这一点:当我在本地计算机上运行时,实例化时它将初始化为0.0。 When i run the same code in my linux test environment it remains null. 当我在linux测试环境中运行相同的代码时,它保持为空。 due to which there is a difference in the retrieved data meaning if i send the CustAmount as null to my Backend i get some data but if i send the CustAmount as 0.0 the query is done on the basis of 0.0 and sends me nothing back. 因此,如果我将CustAmount作为null发送到后端,则会得到一些数据,但是如果我将CustAmount作为0.0发送,则查询将在0.0的基础上完成,并且没有向我发送任何内容。

How is this possible if the code is same, By any chance is it possible that when i do new MyBean() compiled in java 1.5 the double remains null and in 1.6 it gets initialized to 0.0. 如果代码相同,这怎么可能呢?在我用Java 1.5编译新的MyBean()时,任何可能性都可能使double保持为空,而在1.6中,double会初始化为0.0。

I dont know if this is something that happens in two Java versions but thats the only difference on my end. 我不知道这是否发生在两个Java版本中,但这就是我唯一的区别。

Thanks for any hint. 感谢您的任何提示。

Adding code snippet : 添加代码段:

public class MyBean {

private double custAmount;

    public void setCustAmount(double custAmount) {
        this.custAmount = custAmount;
    }
public double getCustAmount() {
        return custAmount;
    }

}

And I just do 我只是做

MyBean mybean =  new MyBean();

its not a Double but a double. 它不是Double,而是Double。 Syed.. Syed ..

A Java double field will be default initialized to 0.0 . Java double字段将默认初始化为0.0 A Java Double field will be default initialized to null . Java Double字段将默认初始化为null These two facts will be true no matter what version of Java you use, and no matter what environment you run it in. 无论您使用哪种Java版本,以及在哪种环境下运行,这两个事实都是正确的。


If you are seeing different behavior in different environments, then the most likely explanation is that you are executing DIFFERENT code in the respective environments (or calling the same code in different ways). 如果您在不同的环境中看到不同的行为,那么最可能的解释是您正在各自的环境中执行不同的代码(或以不同的方式调用相同的代码)。 Posting some code that exhibits the problem may point to some other problem, but I doubt it. 发布一些显示问题的代码可能指向其他问题,但我对此表示怀疑。


Based on your posted code, it is IMPOSSIBLE for custAmount to be null . 根据您发布的代码, custAmountnull是不可能的。 In the case where you are seeing a null , you must be EITHER using a different version of that code, OR the null must be coming from some other source. 在看到null的情况下,您必须使用该代码的其他版本,否则null必须来自其他来源。

I had a similar problem. 我有一个类似的问题。 I thought my Long initializes to 0. But it wasn't initialized itself, but from the <p:selectOneMenu /> on my page. 我以为我的Long初始化为0。但这不是初始化本身,而是从页面上的<p:selectOneMenu />开始的。

            <p:selectOneMenu value="#{myBean.myLongValue}">
                <f:selectItem value="#{null}" itemValue="#{null}" itemLabel="-" />
                <f:selectItems ... />
            </p:selectOneMenu>

So, try to check your page, if you are not setting it somehow from your form. 因此,如果您没有通过表单进行设置,请尝试检查您的页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM