• Post Reply Bookmark Topic Watch Topic
  • New Topic

Head First Java - PhraseOMatic use of .length  RSS feed

 
Dustin Wright
Ranch Hand
Posts: 33
1
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

If this has been discussed, I failed to find it.  I do not understand why this code works.  I'm referring to the lack of parentheses after .length:


Best I can tell from the JavaDaocs, it should be:


The 2nd example will not compile.

Please tell me why.
Thanks.


 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What type is wordListOne, etc?  It's kind of hard to answer your question without the details.

Generally speaking, things like arrays have a parameter called "length". It's not a method call, so you don't need parentesis after it - and in fact, they would be wrong and cause a compiler error.
 
Henry de Deaux
Greenhorn
Posts: 10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all!
What Fred told is right. Considering:


in the first,
You declared an array (typed String). "An object is a class instance or an array. " So you won't find anything in the java API because those "array" are not class instances. You'll find a definition of array in javase specification (the language itself, where all keywords are explained): http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html#jls-10.7 like:
"The public final field length, which contains the number of components of the array. length may be positive or zero."

in the second
.length()
refers to the method of class String object described here : https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#length()
But as wordListOne is not an object, it fails because it's a collection of objects. You could code this to get the number of characters for the third word of your list:


Cheers
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice explanation, but you said,
Henry de Deaux wrote:. . . wordListOne is not an object, it fails because it's a collection of objects. . . .
But an array is an object. It doesn't have a length() method and a String doesn't have a length field. That causes no slight confusion to beginners. If you unzip the source code for String (inside a file called src.zip in your Java® installation folder), you will find the String class contains something like this:-I have omitted about 99.9% of the code, but you can see that Strings hide char[]s and use their length field in the length() method.
 
Henry de Deaux
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
@Campbell Ritchie: Brilliant! I didn't knew about unzipping the source code!! 
Ok, my statement below is wrong  I felt it but couldn't change my post + I couldn't explain why 
But as wordListOne is not an object, it fails because it's a collection of objects.

Now I understand why, thank you!


 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!