length and length() in Java

length and length in Java are used to check the length of the Array and the String. The main difference between length and length() is given below:

array.length : This is a variable used for checking length of arrays like string[], int[], float[].

string.length() : This is a method used for checking length of String objects or StringBuilder class in Java.

The main difference between length and length() is that length applies only to string[], int[], float[] and length() applies to String Object or StringBuilder.

Examples

Some examples on length variable and length() method.

Example for length variable

Following is the example for length variable which is used to check length of string array, integer array or float array.

The output should be:

length of String array is : 4
length of integer array is : 5
length of float array is : 5

Example for length() method

Following is the example for length() method which is used to check length of String Objects or String Builder.

The output should be:

Size of String is : 21

LEAVE A REPLY

Please enter your comment!
Please enter your name here