length vs length() in java


1) length

It is a final variable applicable only for Arrays.

It represent the size of an Array.


//example of length
class Example {

    public static void main(String[] args) {
        int a[] = {2, 3, 23, 5};
        int size = a.length; //using length property
        System.out.println("Size of array is = " + size);
    }
}
Size of array is = 4

1) length()

It is a final method applicable only for String Object.

It represent the number of character present in String.


//example of length()
class Example {

    public static void main(String[] args) {
        String str = "Java is powerful language ";
        int numberOfChar = str.length(); //using length method
        System.out.println("Number of characters  = " + numberOfChar);
    }
}
Number of characters  = 26