• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

get string length

 
Melinda Wilson
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Pie
Posts: 7306
45
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sean Casey
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic