Monday, 23 December 2019

Is length property of array final?


Yes, length property of array is final. As per Java documentation, array has public final field length, which contains the number of components of the array. length may be positive or zero.

App.java
package com.sample.app;

public class App {
 public static void main(String args[]) {
  int[] arr1 = {};
  int[] arr2 = new int[0];

  int[] arr3 = new int[3];
  arr3[0] = 1;

  System.out.println("arr1 length : " + arr1.length);
  System.out.println("arr2 length : " + arr2.length);
  System.out.println("arr3 length : " + arr3.length);
  
 }
}


Run App.java, you will see below messages in console.
arr1 length : 0
arr2 length : 0
arr3 length : 3

Reference

You may like



No comments:

Post a Comment