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

Sybex OCP 11 Programmer I. Chapter 2 review question 8. Defaults for long and float

 
Ranch Hand
Posts: 53
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Jeanne and Scott.
I see that in your book in table “Table 2.3 Default initialization values by type” it is stated that default for long is 0, NOT 0L and default for float is 0.0, not 0.0f.
Nevertheless in Chapter 2 quiz question 8 option class variable of type long defaults to 0 considered to be incorrect and option  class variable of type long defaults to 0L . considered to be correct. It is confusing a bit.


Which of the following are true? (Choose all that apply.)
A class variable of type boolean defaults to 0 .
A class variable of type boolean defaults to false .
A class variable of type boolean defaults to null .
A class variable of type long defaults to null .
A class variable of type long defaults to 0L .
A class variable of type long defaults to 0 .
None of the above
Excerpt From: Boyarsky, Jeanne. “OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide”.

 
Bartender
Posts: 3958
43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree, 0L; is not the same as 0;

 
Marshal
Posts: 80629
471
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The official version, which comes from the Java® Language Specification (=JLS) is,

Each class variable, instance variable, or array component is initialized with a default value when it is created . . .

  •    For type long, the default value is zero, that is, 0L
  •    For type float, the default value is positive zero, that is, 0.0f.
  • Remember that the JLS is the definition of the language, so it will be used for any answers in the test; if the JLS doesn't supply the answer, the API documentation will be regarded as correct. Again, that is the official definition of the classes. 
    As MZ says, there is a difference between 0L and 0; 0 occupies 32 bits all zero and 0L occupies 64 bits all zero. Similarly for floating‑point primitives, 0.0f occupies 32 bits all zero and 0.0 occupies 64 bits all zero. (Not “negative zero”.)
     
    Oksana Cherniavskaia
    Ranch Hand
    Posts: 53
    3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I would also agree that defaults are 0L and 0f. I guess the table in Jeanne's and Scott's book may be modified to reflect that. ))
     
    Campbell Ritchie
    Marshal
    Posts: 80629
    471
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Please search because I seem to remember a very similar question some time ago.
     
    author & internet detective
    Posts: 42135
    937
    Eclipse IDE VI Editor Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Right. i don't consider this an error because we are showing the conceptual value here.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic