• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Simple question about boolean expression

 
Ranch Hand
Posts: 354
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone,

First time posting here and I'm very new to learning java. I have read through forums and I hope I'm using the code tag correctly here.
My question is regarding this snippet of code. The full code is after this. In this snippet how is the value of "length" being set or determined? I don't understand how we know if x is less than or greater than myDogs.length . I hope my question makes sense. This is from the Head First Java book , Chapter 3.

Thank You very much.



 
author
Posts: 23960
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lisa Austin wrote:In this snippet how is the value of "length" being set or determined? I don't understand how we know if x is less than or greater than myDogs.length.



In Java, arrays have a length field which is the size of the array. It is set when the array is instantiated.

Henry
 
Ranch Hand
Posts: 89
Python C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here you create an array: Dog[] myDogs = new Dog[3];
Arrays have some built-in superpowers!
"length" is one of them. This "length" variable stores the max. length of the array. In this case it stores 3.

 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
also. the length of the array is not how many things are IN it, but simply how many it can hold.

Just like an egg carton...it can have twelve spots for eggs, but any/all of those spots can be empty. But the size/length of it will always be 12.
 
Lisa Austin
Ranch Hand
Posts: 354
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You everyone! This really helped me.
 
I think I'll just lie down here for a second. And ponder this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic