Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array.length instead of array.length()?

 
lalitha prasad
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since it's a property & not a method ..
 
lalitha prasad
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why don't we apply the same constraint to String size ie. "sdsds".length().
 
Dominic Steng�rd
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See also http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html#64347
 
Mahesh Bhatt
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic