aspose file tools*
The moose likes Beginning Java and the fly likes array.length  instead of array.length()? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "array.length  instead of array.length()?" Watch "array.length  instead of array.length()?" New topic
Author

array.length instead of array.length()?

lalitha prasad
Greenhorn

Joined: Aug 28, 2004
Posts: 16
Hi,

Anybody please answer the following question.

"If aaaa is an array then why aaaa.length why not aaaa.length()?"

Thanks in advance,
LALITH
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Since it's a property & not a method ..


Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
lalitha prasad
Greenhorn

Joined: Aug 28, 2004
Posts: 16
why don't we apply the same constraint to String size ie. "sdsds".length().
Dominic Steng�rd
Ranch Hand

Joined: Feb 05, 2001
Posts: 186
I agree it seems a bit odd, since the array is an object. But the thing is that an array has fixed length, which means that the size of the array can be calculated when its created, and the size is therefore set in the length variable.


Dominic Steng�rd<br />Sun Certified Java 2 Programmer
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
See also http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html#64347


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
Mahesh Bhatt
Ranch Hand

Joined: Sep 15, 2004
Posts: 88
Hi there,

When we declare/create an array, we also give the size of the array at that time, note that you never use an array untill you have its size already specified someway or the other, However in the case of a String the size may change ( note that you can perform :


here you are trying to increase the current size of the string. However something of this type is not allowed in an array. So the length of an array is a property "length"(the fixed length) however in case of a String its a method(method that does the work of counting all the characters in that String.

Hope this helps you.


Impossible is I M Possible
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by lalitha prasad:
why don't we apply the same constraint to String size ie. "sdsds".length().


Because the Java gods said so, and that's just the way it is. Seriously though, I'm not exactly sure why the Java designers made it this way. It *does* seem a bit odd, but that's the way it is.

Layne


Java API Documentation
The Java Tutorial
 
Don't get me started about those stupid light bulbs.
 
subject: array.length instead of array.length()?