Java Double 被初始化为 0.0
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9769110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Double getting initialized to 0.0
提问by sarmahdi
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. When i run the same code in my linux test environment it remains null. 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.
我有一个 bean,我有一个字段“CustAmount”,它是双倍的,我尝试测试 bean,但我似乎不明白这一点:当我在本地机器上运行时,它在实例化时被初始化为 0.0。当我在我的 linux 测试环境中运行相同的代码时,它仍然为空。因此,检索到的数据存在差异,这意味着如果我将 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 中编译 new MyBean() 时,double 保持为空,而在 1.6 中它被初始化为 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. Syed..
它不是双,而是双。赛义德..
采纳答案by Stephen C
A Java double
field will be default initialized to 0.0
. A Java Double
field will be default initialized to null
. These two facts will be true no matter what version of Java you use, and no matter what environment you run it in.
Javadouble
字段将默认初始化为0.0
. JavaDouble
字段将默认初始化为null
. 无论您使用什么版本的 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 maypoint to some other problem, but I doubt it.
如果您在不同的环境中看到不同的行为,那么最可能的解释是您在各自的环境中执行了不同的代码(或以不同的方式调用相同的代码)。发布一些显示问题的代码可能会指向其他一些问题,但我对此表示怀疑。
Based on your posted code, it is IMPOSSIBLE for custAmount
to be null
. 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.
根据您发布的代码,不可能custAmount
是null
. 在您看到 的情况下null
,您必须要么使用该代码的不同版本,要么null
必须来自其他来源。
回答by xMichal
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.
因此,如果您没有从表单中以某种方式设置它,请尝试检查您的页面。