• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Head First Java - PhraseOMatic use of .length

 
Ranch Hand
Posts: 51
1
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!


 
reply
    Bookmark Topic Watch Topic
  • New Topic