• 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 exercise

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys, i'm an absolute greenhorn both in Java and in forums. I am reading Head First Java second edition and trying to be a good student i'm doing all the exercises. I couldn't really figure out what's going on in this simple program. The code is like this:


   
  The console writes: 9 1

I imagine that the method maybeNew has the command return 1;, so that's why i get that number in the console print, but i can't get my head around the logic of this program. Any help would be more than welcome.

Thank you very moosch
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Which page is it on?
The Mix exercises in HFJ (=your book) are exercises in following the execution of a program. I think the only way to solve them is to write them on paper. You will end up with something like this:-

Start: count = 0
m4a = [][][][][][][][][][][][][][][][][][][][]
x < 9? yes: enter loop
m4a = [0][][][][][][][][][][][][][][][][][][][]
m4a[0].counter increases by 1
m4a = [1][][][][][][][][][][][][][][][][][][][]
... etc

Or maybe it would be quicker to write in a text file on screen. When I wrote the array as [][][]... that isn't quite accurate; it should really be [null, null, null, null...]
 
reply
    Bookmark Topic Watch Topic
  • New Topic