Difference between local variable initialize null and not initialize?

前端 未结 6 1400
太阳男子
太阳男子 2020-12-10 15:09

In Java, what is the difference and best way to do?

Integer x = null; // x later assign some value.
Integer y; // y later initialize and use it.
相关标签:
6条回答
  • 2020-12-10 15:29

    The answer depends on what type of variable are you referring.

    For class variables, there's no difference, see the JLS - 4.12.5. Initial Values of Variables:

    ... Every variable in a program must have a value before its value is used:

    For all reference types (§4.3), the default value is null.

    Meaning, there is no difference, the later is implicitly set to null.

    If the variables are local, they must be assigned before you pass them to a method:

    myMethod(x); //will compile :)
    myMethod(y)  //won't compile :(
    
    0 讨论(0)
  • 2020-12-10 15:33

    Its better to not set it to null, otherwise you can by accident use it and cause NPE. Compiler wont help you with compile error. Only set to null if you want to have logic like if ( x != null ) { /use it/ }

    0 讨论(0)
  • 2020-12-10 15:37

    Internally there is no difference. Also it is a debatable topic.

    Use the local variable only if it is really required. Local variables are mostly in the following scenarios (I may be missing few other).

    As a shorthand or for Readability

    Integer myObject = someObject.someFunction().someOtherFunction();
    

    If we use the syntax like that in many places, code will become clumsy.

    For accessibility

    Object returnObject = null;    
    if(mycondition)
    {
        returnObject = value1;
    }
    else if(secondCondition)
    {
        returnObject = value2;
    }
    
    return returnObject;
    

    The caller of the above code snippet will take decision based on the return value. Honestly speaking i am not seeing other valid use case to declare a variable without initial value.

    Conclusion (Best Practice):

    • Declare local variable only if required
    • Always create local variable with default value.
    0 讨论(0)
  • 2020-12-10 15:40

    Local variables must be assigned to something before they are used.

    Integer x = null;
    myFunction(x);
    // myFunction is called with the argument null
    
    Integer y;
    myFunction(y);
    // This will not compile because the variable has not been initialised
    

    Class variables are always initialised to a default value (null for object types, something like zero for primitives) if you don't explicitly initialise them. Local variables are not implicitly initialised.

    0 讨论(0)
  • 2020-12-10 15:42

    No difference at all. Both cases when ever you want to use it, local variable must be in initialized form(must have a value).

    From Java doc

    Similar to how an object stores its state in fields, a method will often store its temporary state in local variables. The syntax for declaring a local variable is similar to declaring a field (for example, int count = 0;). There is no special keyword designating a variable as local; that determination comes entirely from the location in which the variable is declared — which is between the opening and closing braces of a method. As such, local variables are only visible to the methods in which they are declared; they are not accessible from the rest of the class.

    0 讨论(0)
  • 2020-12-10 15:48

    No differences at all expect for one thing, if you don't initialize it and later on you try to use this variable (without changing the reference) means an error at compilation time, but if you initialize it and you use it later on (without changing the reference) means a NullPointerException.

    I show you with an example.

    Without initializing

    Integer y;
    y.intValue(); // Compilation error
    

    With initializing

    Integer x = null;
    x.intValue(); // You are able to compile it but NullPointerException
    
    0 讨论(0)
提交回复
热议问题