10

I've noticed that when doing the length of an array you write something like:

arrayone.length;

However, for such things as array lists or a string, you write a bracket on the end, such as the following for the length of the String:

stringone.length();

What is the key reason for this and how do you know when to put the brackets or now?

mre
  • 43,520
  • 33
  • 120
  • 170
mino
  • 6,978
  • 21
  • 62
  • 75

8 Answers8

10
.length;

directly accesses a field member.

.length();

invokes a method (i.e. an accessor) to access a field member.

in the case of String, this is to be expected since it's immutable.

mre
  • 43,520
  • 33
  • 120
  • 170
4

Arrays are handled differently than Strings or ArrayLists or anything else that can be counted in Java. An Array is pretty much a native type and it's length can never be changed after it is initialized, so there's no need for encapsulation. The length variable can be directly exposed with no side effects.

The reason why String uses a method instead of a variable is because it internally uses a char[] that it doesn't want to expose publicly (for immutability/encapsulation), so it wraps the length variable in a length() method. It's the same reason ArrayList has a size() method instead of a length variable.

The only time you'll use the variable instead of the method is with arrays. Everything else will be methods. That is, you'll use the brackets for everything except arrays.

Sastrija
  • 3,284
  • 6
  • 47
  • 64
Robert Rouhani
  • 14,512
  • 6
  • 44
  • 59
2

The only true way to know when to use which one is experience. Though an IDE with autocompletion will usually help you out when you don't remember.

For the most part (not always) array.length, System.out, and System.err are the most common 3 you'll run into that are actually member access instead of method calls.

rfeak
  • 8,124
  • 29
  • 28
0

.length() is a method of a String class and which returns the number of characters in the string.

.length will give the number of elements stored in an array.

public class length
{
public static void main(String args[])
{
String x="test";
int a[]={1,2,3,4};
System.out.println(x.length());
System.out.println(a.length);
}
}

// output
4
4
Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
MWeller
  • 105
  • 1
  • 13
0

Array.length is a property of that Array, similar to a variable reference.

ArrayList.size() is an actual method call to the array list object.

Sastrija
  • 3,284
  • 6
  • 47
  • 64
0
int[] myArray = new int[10];
String myString = "hello world!";
List<int> myList = new ArrayList<int>();

myArray.length    //gives the length of the array
myString.length() //gives the length of the string
myList.size()     //gives the length of the list

Its very likely that strings and arrays were designed at different times and hence ended up using different conventions. One justification is that since Strings use arrays internally a method length() was used to avoid duplication of the same information. Ultimately this is just an inconsistently that evolved that would definitely be fixed if the language were ever redesigned from the ground up. :D

Gordon Gustafson
  • 40,133
  • 25
  • 115
  • 157
0

The main difference is that in the A) first case its Array Type for example int[], Object[], double[], ect.. that has a public field called lenght and the B) second case is a Object String that has a function called length(), the function could of been called getLength() or something else. The array type public field length is probably a hangover from C++ way of doing things.

Array Types have the following:

  • The public final field length, which contains the number of components of the array (length may be positive or zero)
  • The public method clone, which overrides the method of the same name in class Object and throws no checked exceptions
  • All the members inherited from class Object; the only method of Object that is not inherited is its clone method

Take a look at this, Array Types.

Dimitry
  • 4,503
  • 6
  • 26
  • 40
0

length is a pseudo-data member reference, and only works for (small-a) arrays (ignoring classes that you may define that implement the member).

Everything else is an object of a class, and all JDK classes that have this concept define either length() or size() instance methods. (I do wish they'd been consistent and picked one or the other, but that's water over the bridge.)

Hot Licks
  • 47,103
  • 17
  • 93
  • 151