Length of Array and String in Java Program

A simple java program to find the length of the array and string a single program. Using this program we can calculate array and string in the array in java.  The array length in Java refers to the maximum number of elements that an array can hold. The length of an array cannot be determined using any predefined method. The array attribute length in Java can be used to determine the array length.

public class Main
{
public static void main(String[] args)
{

int[] array = new int[4];
System.out.println("The size of the array is " + array.length);
String str = "Inlarn.com";
System.out.println("The size of the String is "+ str.length());
}
}

 

Output:

The size of the array is 4
The size of the String is 10

What is length() in java

The length() method of the String class is a static method. The length() method returns the length of a string object or the number of characters it contains. This function is used by the String class because the length of a string can be changed using various operations on an object.

What is array length in java

The array length in Java refers to the maximum number of elements that an array can hold. The length of an array cannot be determined using any predefined method. The array attribute length in Java can be used to determine the array length.

Difference between size and length in array in java

size() is a method defined in the java. util. Collection package, which is inherited by all data structures in the standard library. length is a field on any array (arrays are objects, although you don’t generally see the class), and length() is a java function.