BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java program to print number of elements in an array

Last Updated: June 26, 2022 by Chaitanya Singh | Filed Under: Java Examples

In this tutorial, you will learn how to find the number of elements present in an array.

Illustration:

Array: {1, 2, 3, 4, 5}
Number of elements  = count of the elements  = 5

Example: Program to print number of elements in an array

In this example, we have two arrays. We are finding the count of the elements in both the arrays. The steps to count the number of elements is same in any type of array, you can simply use .length property of the array to quickly find the size of the array.

public class JavaExample {
  public static void main(String[] args) {
    //Initializing an int array
    int [] numbers = new int [] {2, 4, 6, 8, 10, 12};
    // We can use length property of array to find the count of elements
    System.out.println("Number of elements in the given int array: " + numbers.length);

    //Initializing a String array
    String [] names = new String [] {"Rick", "Luna", "Steve", "John"};
    System.out.println("Number of elements in the given String array: " + names.length);
  }
}

Output:

Number of elements in the given int array: 6
Number of elements in the given String array: 4

Recommended Java Programs

  1. Java program to find smallest number in an array
  2. Java program to find the largest element of an array
  3. Java program to print the duplicate elements of an array
❮ Java TutorialJava Programs ❯

Top Related Articles:

  1. Java Program to Calculate average using Array
  2. Java program to find smallest number in an array
  3. Java Program to right rotate the elements of an array
  4. Java Program to left rotate the elements of an array
  5. Java Program to count the frequency of each element in array

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap