my dog learned polymorphism
The moose likes Beginning Java and the fly likes get string length Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "get string length" Watch "get string length" New topic

get string length

Melinda Wilson
Ranch Hand

Joined: Jul 05, 2001
Posts: 35
Beginner java programmer.
I need to get the string length of a string array:
This generated an error:
int astrArrayLength = astrArray.length(); //get string length
for (int i = 0; i < astrArrayLength; i++)

This got rid of the error:
int astrArrayLength = astrArray.length; //get string length
for (int i = 0; i < astrArrayLength; i++)
Is length relaced with length()?
Thank you.
Junilu Lacar

Joined: Feb 26, 2001
Posts: 6323

Is length relaced with length()?
No, I doubt that the compiler does anything special behind the scenes. Just think of any array in Java as an Object that has a length attribute. It's just one of those little quirks that you have to remember: arrays have a length attribute while String has a length() method. Go figure.

Junilu - [How to Ask Questions] [How to Answer Questions]
Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
array.length will return the length of an array.
string.length() is a method that returns the length of a string.
Every array inherently knows its own length. It's a property of an array and can be shown with the array.length . I know it's a bit confusing, but just remember that's how it is.
I agree. Here's the link:
subject: get string length
It's not a secret anymore!