public course Test { public revolution void main(String<> args) { String<> people = "Bob", "Billy", "Jim"; int amount = 0; for(int ns = 0; ns My concern is why people.length() is .length() and also not .length. I thought to obtain the length of an array you usage .length and not .length(). Execute you usage .length() because you space trying to get the length of a string and not one array? P.S. Ns am a beginner, for this reason this may seem really obvious to you, however it isn"t to me.

You are watching: .length vs .length()


*

*

They"re two fully different things.

.length is a residential or commercial property on arrays. The isn"t a technique call.

.length() is a technique call top top String.

You"re seeing both since first, you"re iterating end the length of the array. The materials of the range are String, and also you want to add up every one of their lengths, so girlfriend then call the length on each individual wire in the array.


*

.length is variety property. .length() is a method for the course String (look here).

When you room looping, you room looping because that the size of the selection using people.length.

But once you space using people.length(), you room accessing the string in ~ that position of the array, and getting the length of the string, because of this using the .length() method in the cable class.

See more: Welcome To Jacob &Quot;Jake&Quot; Olsen, Jacob: Name Meaning, Origin, Popularity

However, simply to confused you more, a String in ~ its main point is just range of chars (like this: char<>). One can make the dispute that .length should work together well, considering it is variety of characters, however, it is a class and also that is the factor .length will not work. Showing empty parameters reflects it"s a method, and showing no parameters mirrors that it"s a building (like a revolution variable in a class).


*

.length is because that Arrays, and .length() because that Strings

Array"s size is a field, while the String"s length is in a duty - .length()


*

Not the price you're looking for? Browse other questions tagged java arrays or asking your own question.


site style / logo © 2021 stack Exchange Inc; user contributions licensed under cc by-sa. Rev2021.9.20.40247


your privacy

By click “Accept every cookies”, friend agree stack Exchange deserve to store cookies on your machine and disclose details in accordance through our Cookie Policy.