Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't this local variable initialized in Java?

Tags:

java

I am learning Java and I know that I must initialize a local variable when I use it. However, I just found a code from the book and the code is: I wonder why in this case the variable volume is not initialized?

public static double cubeVolume(double sideLength)
{
    double volume;
    if (sideLength>=0)
    {
        volume=sideLength*sideLength*sideLength;
    }
    else
   {
      volume=0;
   }
   return volume;
}
like image 334
user3504084 Avatar asked Sep 15 '26 09:09

user3504084


2 Answers

The rule is that it must be initialised before it is used, since on both branches of the if statement volume has been initialised before it is returned (aka used) the compiler can guarantee that it will have been initialised before being used.

If you attempted to use volume before the if statement you will again receive that compilation error. Equally if it wasn't initialised on all branches (in this case both sides of the if statement) you would get the error.

Examples

The following examples may give some incite into when this is likely to be a problem:

ok (but pointless):

double volume; //<--declared
volume=6;  //<--initialised
double volumeUsed=2*volume;

ok:

boolean useUpper=true; //<-- useUpper declared and initialised
double volume;
if (useUpper){
    volume=6; //<--initialised
}else{
    volume=7; //<--initialised
}
double volumeUsed=2*volume;

not ok (volume not known when used):

double volume;
double volumeUsed=2*volume;

not ok (volume may not be known when used - if useUpper were false):

boolean useUpper=true;
double volume;
if (useUpper){
    volume=6; //<--initialised
}
//volume may not be initialised
double volumeUsed=2*volume;

not ok: used before being initialised

double volume;
double volumeUsed=2*volume;
if (sideLength>=0)
{
    volume=sideLength*sideLength*sideLength; //<--initialised (too late)
}
else
{
   volume=0;
}
return volume;
like image 60
Richard Tingle Avatar answered Sep 17 '26 23:09

Richard Tingle


Because the writer doesn't have any value for volume until the if is evaluated.

Other options include:

double volume = 16.0; // Random. Wrong. Bug-prone.

or:

double volume = 0.0; // Conveys that 0 is somehow a valid value. 
// or that another value might not be entered.

Of course in this case 0.0 is indeed a perfectly valid value, so this option is all right:

double volume = 0.0;
if(sideLength >= 0) {
    volume = sideLength * sideLength * sideLength;
}

But this arguably raises the question, why did we set volume twice? I think not a big deal, but some coders may not prefer it. Now this is getting advanced, but if volume needed to take the final modifier, e.g. because it appears in a closure later, this would be illegal:

final double volume = 0.0;
if(sideLength >= 0) {
    volume = sideLength * sideLength * sideLength;  // compiler error
}

If the author prefers terseness (which I do not recommend here), this can all be done in one line as follows:

double volume = sideLength >=0 ? sideLength * sideLength * sideLength : 0;

And just one more example to be clever (this is terrible, don't do this):

double volume = Math.max(sideLength * sideLength * sideLength, 0);
// does not generalize to even dimensions
like image 31
djechlin Avatar answered Sep 18 '26 01:09

djechlin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!