length of array & String usage

M

Mike

Hi

In array, if we want to know the number of elements of a array, we can
use

a.length

However, if we want to know the number of characters of a string, we
use

a.length()

Why is there no parenthesis in the former, and is there parenthesis in
the latter?

thank you in advance.

Mike
 
E

EricF

Hi

In array, if we want to know the number of elements of a array, we can
use

a.length

However, if we want to know the number of characters of a string, we
use

a.length()

Why is there no parenthesis in the former, and is there parenthesis in
the latter?

thank you in advance.

Mike

Because the Java API is inconsistent. For lists, you use list.size().

But to answer your question, for arrays, length is an attribute. For strings,
length is a method.

Eric
 
J

Juan Pedro Villa

Hi

In array, if we want to know the number of elements of a array, we can
use

a.length

However, if we want to know the number of characters of a string, we
use

a.length()

String implements the CharSequence interface, which is a readable
sequence of char values. It has a method (length) that returns the
length of the character sequence (in this case, the length of the
String). String is not an array, but it is a CharSequence, so you use
length(). But you can also use just the attribute length if you create
a char[] from the original String with the method toCharArray(). And
for lists, stacks, vectors, ... you use size() because they implement
the interface Collection (which has the method size()).
 
M

Mike

In array, if we want to know the number of elements of a array, we can
use

However, if we want to know the number of characters of a string, we
use
a.length()

String implements the CharSequence interface, which is a readable
sequence of char values. It has a method (length) that returns the
length of the character sequence (in this case, the length of the
String). String is not an array, but it is a CharSequence, so you use
length(). But you can also use just the attribute length if you create
a char[] from the original String with the method toCharArray(). And
for lists, stacks, vectors, ... you use size() because they implement
the interface Collection (which has the method size()).

thank you.
 
L

Lew

Because the Java API is inconsistent. For lists, you use list.size().

It isn't an issue of API consistency. Arrays are one of the essential
elements of Java - they're not called "java.lang.array"s. They're part of the
language, not the API. The Collections are not part of the language.

If both were in the language, or both in the API, we could discuss consistency.

In any event, EricF's answer is the answer:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,609
Members
45,253
Latest member
BlytheFant

Latest Threads

Top