检查变量是否在 Java 中初始化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34476792/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:56:02  来源:igfitidea点击:

check if variable is initialized in Java

java

提问by mr_azad

Is there a way in Java to check the variable is initialized of not inside a class. I found for javascriptbut not for Java. Hear is an example of what I am looking for.

Java 中有没有办法检查变量是否在类内初始化。我为javascript而不是为 Java找到了。听到是我正在寻找的一个例子。

private float[] Average;
private void Check(){
if(/*variable is not initialized*/){ Average = new float[4];}
}

回答by Andrea Dusza

You can use if (Average == null)to check if it's null, but you cannot tell if it was explicitly set to null or just null by default. This works for every Object type (arrays are also objects), because objects' default value is null. The 8 primitive types (int, byte, float, char, long, short, double and boolean) however cannot be null. E.g. an intis 0 by default if you do not assign a value to it.

您可以使用if (Average == null)它来检查它是否为空,但您无法判断它是明确设置为空还是默认设置为空。这适用于所有对象类型(数组也是对象),因为对象的默认值为 null。然而,8 种基本类型(int、byte、float、char、long、short、double 和 boolean)不能为 null。例如,int如果不为其分配值,则默认情况下为 0。

回答by Anthony Raymond

Arrays in java works like objects (they are not primitive types).

java 中的数组就像对象一样工作(它们不是原始类型)。

So yes, you can check if your array was initialized or not with :

所以是的,您可以使用以下命令检查您的数组是否已初始化:

private void check(){
    if(average == null){
        average = new float[4];
    }
}





A better solution (if you know the array size when instantiate)

更好的解决方案(如果您知道实例化时的数组大小)

But in my opinion, you'd better initialize the variable in your class constructor, like bellow :

但在我看来,您最好在类构造函数中初始化变量,如下所示:

public class MyClass {
    private float[] average;

    public MyClass(int arraySize) {
        this.average = new float[arraySize];
    }
}

This way, you'll be sure it is initialized everytime a MyClassobject is created.

这样,您将确保每次MyClass创建对象时都会对其进行初始化。

An even better solution (even if you don't know the array size when instantiate)

一个更好的解决方案(即使您在实例化时不知道数组大小)

If you don't know the size of the array, i'd better use a List:

如果您不知道数组的大小,我最好使用一个List

public class MyClass {
    private List<Float> average;

    public MyClass() {
        this.average = new ArrayList<>();
    }
}

Lists are resized automatically as they goes full.

列表在变满时会自动调整大小。

回答by hrust

If you are worried about the variable outside the class definition then you should check if there are nullor not.

如果您担心类定义之外的变量,那么您应该检查是否存在null

nullwill indicate that variable is not initialized. In case of primitive types, such as int, doubleand similar you will be notified by the compiler that particular variable is not initialized.

null将指示变量未初始化。在原始类型,如情况下intdouble和类似的,你会被编译器告知特定的变量未初始化。

In classes fields get initialized with default values even before constructor execution.

即使在构造函数执行之前,类中的字段也会使用默认值进行初始化。

回答by Mick Mnemonic

You could declare the field as final. Doing that, you have toinitialize it, either in the constructor or during variable declaration. Using a finalfield would make your variable-not-initialized check redundant:

您可以将该字段声明为final. 这样做,您必须在构造函数中或在变量声明期间对其进行初始化。使用final字段会使未初始化的变量检查变得多余:

private final float[] average = new float[SIZE_OF_ARRAY];

or

或者

private final float[] average;

public MyClass(int sizeOfArray) {
    this.average = new float[sizeOfArray];
}