Java Double初始化为0.0 [英] Java Double getting initialized to 0.0

查看:1422
本文介绍了Java Double初始化为0.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个bean,我有一个字段CustAmount,它是双倍的,我尝试测试bean,我似乎不明白这一点:当我在我的本地机器上运行时,它在实例化时被初始化为0.0。当我在我的linux测试环境中运行相同的代码时,它仍然是null。由于检索到的数据存在差异,这意味着如果我将CustAmount作为null发送到我的后端,我会获得一些数据,但如果我将CustAmount发送为0.0,则查询将在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. 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.

如果代码相同,这怎么可能呢?有可能当我在java 1.5中编译新的MyBean()时,double仍然是null并且在1.6中它被初始化为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版本中发生的事情,但这是我最终的唯一区别。

I dont know if this is something that happens in two Java versions but thats the only difference on my end.

感谢任何提示。

添加代码段:

public class MyBean {

private double custAmount;

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

}

我只是做

MyBean mybean =  new MyBean();

它不是Double而是double。
Syed ..

its not a Double but a double. Syed..

推荐答案

Java double 字段将默认初始化为 0.0 。 Java Double 字段将默认初始化为 null 。无论您使用什么版本的Java,无论您在何种环境中运行它,这两个事实都将成立。

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.

如果您在不同的环境中看到不同的行为,那么最可能的解释是您在相应的环境中执行不同的代码(或以不同的方式调用相同的代码)。发布一些显示问题的代码可能指向其他一些问题,但我对此表示怀疑。

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.

根据您发布的代码, custAmount 对于 null 是不可能的。在您看到 null 的情况下,您必须使用该代码的不同版本,或者 null 必须来自其他来源。

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.

这篇关于Java Double初始化为0.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆