• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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: 79949
396
  • 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.
 
Because those who mind don't matter and those who matter don't mind - Seuss. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic