This guide will show you how to find the length of an array in Java. This method is straightforward, but once you understand how it works, you can use it in all kinds of more complicated contexts. Follow the steps below to find array size in Java.

Steps

  1. 1
    Open the IDE. Open your IDE of choice, or write the code and execute it through the Command Prompt.
  2. 2
    Copy and paste the following code:
      class Size
      {
      static int[] numbers = {1, 225, 231, 4, 675};
      public static void main(String[] args)
      {
      int size = numbers.length;
      System.out.println("The size of array is : "+size);
      }
      }
      
    Advertisement
  3. 3
    Run the program. The output will be The size of array is : 5.
  4. 4
    Understand what the program does. Once you understand how the .length statement works, you can use it in other contexts easily.
    • You declare a Integer array numbers with the values : [1. 225, 231, 4, 675].
    • Next, you use the statement "numbers.length" to get the size of Integer array numbers and assign it to Integer variable size.
    • Then you print the size of the Integer array numbers.
  5. Advertisement

Things You'll Need

  • JDK installed
  • A Java IDE (optional)


About This Article

wikiHow is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, volunteer authors worked to edit and improve it over time. This article has been viewed 71,507 times.
How helpful is this?
Co-authors: 4
Updated: December 2, 2021
Views: 71,507
Categories: Java
Advertisement