• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Mouse Eating Non-existent Cheese Bites?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am learning Java via Sybex OCA Book.

I have reached while loop description which has the following example on page 77:



What confused me was that bitesOfCheese was never explicitly initialized, however still gets value of 5 assigned upon execution of eatCheese and therefore can legally participate in while expression check.
Could you please explain why does it automatically gets assigned the value of 5?

Thank you.
 
Bartender
Posts: 209
14
Mac OS X IntelliJ IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to have a look at the rest of the code which you haven't posted here. bitesOfCheese is a parameter so I guess the value of 5 was passed to this method.
 
Marshal
Posts: 80669
478
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please provide more details of the book, including author. Is that Boyarsky and Selikoff? I don't have a copy of that book.
I suggest you go through the code first and indent it correctly. People who write cert exam practice questions sometimes indent their code inconsistently to make the question harder. Did you notice that bitesOfCheese is a method parameter in line 2? Is the final output 5? In that case there must be some code calling eatCheese(10); Such a method call definitely assigns a value to that parameter.
reply
    Bookmark Topic Watch Topic
  • New Topic