Java length vs length(): What's the difference?

Java length vs length() explained

The key difference between Java’s length variable and Java’s length() method is that the Java length variable describes the size of an array, while Java’s length() method tells you how many characters a text String contains.

The Java length method

To get information about an object in Java, you typically go through a public method.

To get the size of a String in Java, length() is the method you call. The String’s length() method will tell you how many characters a String contains.

A length() example for Java Strings

Here is a simple example of the Java length() method being used to get the number of characters in a String:

String simpleString = "Length example";
int listSize = simpleString.length();

Notice how the Java length() method has round brackets at the end of it. This is one of the primary differences between the length() method and Java’s length property.

The Java length property

To get the size of an array in Java, you access the array’s length property.

Notice how we have switched from talking about Java’s length() method to Java’s length property. This is a primary difference between the two uses of the word length in Java.

A length example for Java arrays

Here is an example of the Java length method being used to get the number of elements in an array:

int[] simpleArray = {1,2,3,4,5};
int simpleArraySize = simpleArray.length;

Differences between Java length and length()

Here are the key differences between the two Java length constructs and how they are used in code:

  1. The Java length property is used with the String class
  2. The Java length method is used with the array class
  3. The Java length property returns the size of an array
  4. The Java length method returns the number of characters in a text String
  5. The Java length method must have round brackets at the end
  6. The Java length property throws a compile error if round brackets are added
  7. The Java length method is a good example of data encapsulation
  8. The Java length property has its roots in C and C++ programming

Similarities between the Java length property and method

The biggest similarity between the two Java length constructs, be it for Strings or arrays, is that is used to find the number of elements an object contains.

They key difference between Java length and length() constructs is that the method works with the String class and the property works with arrays.

This subtle difference can often result in confusion for new developers who are just getting used to Java syntax. This is because:

  • In Java, a method must have round brackets at the end of it: length()
  • In Java, properties are not followed by brackets or braces: length

If round brackets are put at the end of a property, or left off a method name, a Java compile time error will occur. And sadly, the associated error message isn’t very helpful.

How to compare Java length versus length() for arrays and Strings.

Java length compile time errors

If round brackets are left off a Java length() method call, the following code error will occur:

length cannot be resolved or is not a field

If round brackets are added to a Java length property, the following code error will occur:

Cannot invoke length() on the array type int[]

If Java length and length() syntax is mixed up, compile time errors will occur.

Why do arrays and Strings use the Java length differently?

Java values the encapsulation of data and properties, which means any information you access about an object should come through a public method.

The Java String’s length() method complies with this best practice. So why doesn’t an array?

The difference harks back to Java’s C based heritage.

C++, Java and the length of an array

The Java programming language was written in such a way that it would make it easy for developers who program in C and C++ to adopt the language.

Several programming constructs in Java can be mapped directly back to how C and C++ programs work. One of those constructs is Java’s main method, and another is the fact that the size of an array is accessed through the length property, not the length method.

The difference between Java length() versus length in Java can be confusing for new developers. But it’s just a one-off rule about how arrays work differently from Strings that Java developers just have to get used to.

You’ll get used to it eventually. Trust me.

Java length Comparison Chart
Java length() method Java length property
Primarily used by the String class Used by Java arrays
Must have round brackets at the end Must not have round brackets at the end
Syntax: stringVariable.length(); Syntax:  arrayVariable.length;
Returns the number of characters in a String Returns the size and capacity of an array
The ‘length cannot be resolved’ compile error can occur on misuse The ‘cannot invoke length() on array’ compile error can occur on misuse
SearchAppArchitecture
SearchSoftwareQuality
SearchCloudComputing
SearchSecurity
SearchAWS
Close