permaculture playing cards*
The moose likes Beginning Java and the fly likes Length of an array and String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Length of an array and String" Watch "Length of an array and String" New topic
Author

Length of an array and String

Vallabhaneni Suresh Kumar
Ranch Hand

Joined: Mar 01, 2004
Posts: 68
Why we are using arrayname.length to find the length of an array and string.length() for a String? Why we are not using length() method for an array?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41052
    
  43
Because an array is not strictly speaking an object (i.e. it does not extend Object), so you can't call methods on it, and it shouldn't look like you can.


Ping & DNS - my free Android networking tools app
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
An array is an object. Try this


But, why you refer it as a property (length), instead of a method (length()) - no clue. Let me think about it.
[ July 22, 2005: Message edited by: Mani Ram ]

Mani
Quaerendo Invenietis
Steve Simon Joseph Fernandez
Ranch Hand

Joined: Jul 17, 2005
Posts: 35
Hi,

I may be wrong on this, but this is what I think:

IMHO, String.length() has to be a function and not a public final variable because String is implemented by java code itself. So, it doesn't know the length of the string until it creates it, and thus it can't provide it as a final variable. On the other hand, arrays for primitive types are implemented via an external platform-dependent dll, which returns the specified object. Thus, java code will know the length of an array when it gets it back from the dll, and thus can provide a public final property for the same.

P. S. Don't kill me if I'm wrong ;-)

_steve.
Niki Nono
Ranch Hand

Joined: Mar 20, 2005
Posts: 256
The array.length is a variable which has the size of the array.
the string.length() is a function which calculates the length of the string.


Life called,so here I am.<br />Cheers<br />Niki.:-)
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Niki Nono:

the string.length() is a function which calculates the length of the string.


If you take a look at the implementation, it just returns the value of the private "count" field. The count field is only assigned in constructors.

BUT - for the count field to be public, it would have been vital to also make it final. That's no problem for arrays, which can only be constructed in very specific ways. For String it might constrain the class' design too much to be a good idea.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Length of an array and String
 
Similar Threads
Sorting an Array, Re-assign
multidimensional array length
About array in Java
Question about assignment 1b.
String.length() vs. Arrays.length