Java - What is the output: array length access?


What is the output of the following code?

int[] salary;
int len = salary.length;

Click to view the answer

// A runtime error. salary is not referring to any array object yet.
int len = salary.length;


length is the property of the array object you create.

Until you create the array object, you cannot use its length property.


public class Main {
  public static void main(String[] args) {
    int[] salary;
    // int len = salary.length;
    // Create an int array of length 1000 and assign its reference to salary
    salary = new int[1000];

    // Correct. len has a value 1000
    int len = salary.length;
    System.out.println(len);//from  w  w w.  j  a v a  2s  .  c  o  m