• Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple question about boolean expression  RSS feed

 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • 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.



 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Ashwin Rao
Ranch Hand
Posts: 89
C++ Java Python
  • Mark post as helpful
  • send pies
  • 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.

 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You everyone! This really helped me.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!