array.length : length is a final variable applicable for arrays. With the help of length variable, we can obtain the size of the array.
string.length() : length() method is a final variable which is applicable for string objects. length() method returns the number of characters presents in the string.
length vs length()
- The length variable is applicable to array but not for string objects whereas the length() method is applicable for string objects but not for arrays.
- Examples:
// length can be used for int[], double[], String[] // to know the length of the arrays. // length() can be used for String, StringBuilder, etc // String class related Objects to know the length of the String
- To directly accesses a field member of array we can use .length; whereas .length() invokes a method to access a field member.
Example:
// Java program to illustrate the // concept of length // and length() public class Test { public static void main(String[] args) { // Here array is the array name of int type int[] array = new int[4]; System.out.println("The size of the array is " + array.length); // Here str is a string object String str = "GeeksforGeeks"; 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 13
Practice Questions based on the concept of length vs length()
Lets have a look on the output of the following programs?
- What will be the output of following program?
publicclassTest {publicstaticvoidmain(String[] args){// Here str is the array name of String type.String[] str = {"GEEKS","FOR","GEEKS"};System.out.println(str.length);}}chevron_rightfilter_noneOutput:
3
Explanation: Here the str is an array of type string and that’s why str.length is used to find its length.
- What will be the output of following program?
publicclassTest {publicstaticvoidmain(String[] args){// Here str[0] pointing to a string i.e. GEEKSString[] str = {"GEEKS","FOR","GEEKS"};System.out.println(str[0].length);}}chevron_rightfilter_noneOutput:
error: cannot find symbol symbol: method length() location: variable str of type String[]
Explanation: Here the str is an array of type string and that’s why str.length() CANNOT be used to find its length.
- What will be the output of following program?
publicclassTest {publicstaticvoidmain(String[] args){// Here str[0] pointing to String i.e. GEEKSString[] str = {"GEEKS","FOR","GEEKS"};System.out.println(str[0].length());}}chevron_rightfilter_noneOutput:
5
Explanation: Here str[0] pointing to String i.e. GEEKS and thus can be accessed using .length()
This article is contributed by Bishal Kumar Dubey. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- StringJoiner length() method in Java
- StringBuilder length() in Java with Examples
- How to find the length or size of an ArrayList in Java
- Difference between length of Array and size of ArrayList in Java
- Year length() method in Java with Examples
- BitSet length() Method in Java with Examples
- AtomicLongArray length() method in Java with Examples
- AtomicIntegerArray length() method in Java with Examples
- Month length() method in Java
- CharBuffer length() methods in Java with Examples
- How to determine length or size of a String in Java?
- How to determine length or size of an Array in Java?
- AtomicReferenceArray length() method in Java with Examples
- Java program to print Even length words in a String
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.Lang.Float class in Java
- Java.io.BufferedInputStream class in Java
- Java.io.ObjectInputStream Class in Java | Set 1
- Java.util.BitSet class in Java with Examples | Set 1
- Java.io.File Class in Java

