Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is String.length() a method?

Tags:

java

string

If a String object is immutable (and thus obviously cannot change its length), why is length() a method, as opposed to simply being public final int length such as there is in an array?

Is it simply a getter method, or does it make some sort of calculation?

Just trying to see the logic behind this.

like image 961
Acidic Avatar asked Jan 03 '12 23:01

Acidic


People also ask

Why is string length a method?

What is String “Length” Method in Java? This function is used to get the length of string in Java. The string length method returns the number of characters written in the String. This method returns the length of any string which is equal to the number of 16-bit Unicode characters in the string.

Why is length a method?

The length() method is a static method of String class. The length() returns the length of a string object i.e. the number of characters stored in an object. String class uses this method because the length of a string can be modified using the various operations on an object.

What does method length () do in string class?

The Java String class length() method finds the length of a string. The length of the Java string is the same as the Unicode code units of the string.

Is length a method in Java?

length() in Java is a final method, which is applicable for string objects. You can use it to find the number of characters in a string. For example, string. length() will return the number of characters in “string.”


2 Answers

Java is a standard, not just an implementation. Different vendors can license and implement Java differently, as long as they adhere to the standard. By making the standard call for a field, that limits the implementation quite severely, for no good reason.

Also a method is much more flexible in terms of the future of a class. It is almost never done, except in some very early Java classes, to expose a final constant as a field that can have a different value with each instance of the class, rather than as a method.

The length() method well predates the CharSequence interface, probably from its first version. Look how well that worked out. Years later, without any loss of backwards compatibility, the CharSequence interface was introduced and fit in nicely. This would not have been possible with a field.

So let's really inverse the question (which is what you should do when you design a class intended to remain unchanged for decades): What does a field gain here, why not simply make it a method?

like image 111
Yishai Avatar answered Sep 30 '22 23:09

Yishai


Perhaps a .length() method was considered more consistent with the corresponding method for a StringBuffer, which would obviously need more than a final member variable.

The String class was probably one of the very first classes defined for Java, ever. It's possible (and this is just speculation) that the implementation used a .length() method before final member variables even existed. It wouldn't take very long before the use of the method was well-embedded into the body of Java code existing at the time.

like image 29
Greg Hewgill Avatar answered Oct 01 '22 00:10

Greg Hewgill