Sum of No Elements

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

for ( double val : array )
total += val ;

Sum of No Elements

Here is the revised program:

class SumArrayRevised
{

  public static void main ( String[] args ) 
  {
    double[] array =  {} ;

    // declare and initialize the total
    double    total =    0.0 ;

    // add each element of the array to the total
    for ( double val : array )
      total += val ;
      
    System.out.println("The total is: " + total );
  }
}      

QUESTION 20:

But the array has no elements!!   Will the program compile?   Will the program run?   What is the sum of an array with no elements?