Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

length of array & String usage

Thread view: 
Mike - 05 Jun 2007 03:43 GMT
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
EricF - 05 Jun 2007 04:07 GMT
>Hi
>
[quoted text clipped - 14 lines]
>
>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
Lew - 05 Jun 2007 12:50 GMT
Mike <SulfateIon@gmail.com> wrote:
>> In array, ... a.length
>>
>>  of a string, ... a.length()
>>
>> Why is there no parenthesis in the former, and is there parenthesis in
>> the latter?

> 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:
> But to answer your question, for arrays, length is an attribute. For strings,
> length is a method.

Signature

Lew

Juan Pedro Villa - 05 Jun 2007 05:09 GMT
> Hi
>
[quoted text clipped - 7 lines]
>
> 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()).
Mike - 05 Jun 2007 07:14 GMT
> > Hi
>
[quoted text clipped - 16 lines]
> for lists, stacks, vectors, ... you use size() because they implement
> the interface Collection (which has the method size()).

thank you.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.