开发者

Why instantiate variables with a null value

开发者 https://www.devze.com 2023-01-23 07:36 出处:网络
In many pieces of example code I see variables instatiated with null values and later assigne开发者_JAVA百科d more meaningful values.

In many pieces of example code I see variables instatiated with null values and later assigne开发者_JAVA百科d more meaningful values.

I was just wondering why people may do this. I'm guessing try-catch blocks may well come into this, but I also see variables instantiated will null values inside of a try block.

(I'm sure this is a fairly language agnostic question, but just for reference I program almost entirely in Java)

All insights appreciated!


The Java compiler detects in certain cases if a variable has not been initialized in all possible control flows and prints an error. To avoid these error messages, it's necessary to explicitly initialize the variable.

For example in this case:

  public Integer foo() {
    Integer result;

    if (Math.random() < 0.5) {
      result = 1;
    }

    return result;
  }

The compiler would give this error message: "The local variable result may not have been initialized".

Here is what the Java Language Specification says:

Each local variable (§14.4) and every blank final (§4.5.4) field (§8.3.1.2) must have a definitely assigned value when any access of its value occurs. A Java compiler must carry out a specific conservative flow analysis to make sure that, for every access of a local variable or blank final field f, f is definitely assigned before the access; otherwise a compile-time error must occur.

Note that (in contrast to fields!) local variables are not automatically initialized to null.


Personally, I don't like it for member fields that will take a value in the constructor. There was a time when I thought it was just nice to be explicit, but there's actually a bytecode difference between assigning a field to null explicitly or the field taking the default value.

Even for fields that do start out as null (and then gain a value sometime after initialization) I'm not a big fan of them. It usually just highlights misunderstanding from the developer that did that.

The try-catch relevance is in cases like this:

Reader r = null;
try {
    r = ...;
    //do something that could throw an exception
} finally {
    if ( r != null ) { //wouldn't compile without assignment to null
        r.close();
    }
}

Because r is a local variable here it needs a value before it can be used, so the assignment is necessary. If the assignment to null happens in the same code path as its subsequent reassignment and/or use, that's probably just misleading redundancy.


YES, thought about this question many times.

For now, I usually give a initial value null for a object, and a meanless value for simple type. The reason, sometimes you dont cant sure that the JRE gives initial value to the variable. And sometimes you need to judge weither the variable is null, maybe you forget init it. Some bugs is easy to find, If gives variables a initial value.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号