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

Possible errata for OCP 1Z0-815 study guide

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chapter 2, Page 53, Table 2.3 (Default initialization values by type)

According to Oracle docs, long variables default to 0L instead of 0, float variables default to 0.0f instead of 0.0, and double variables default to 0.0d instead of 0.0, though the output results are 0, 0.0, and 0.0, respectively.

So for Review Question #6 on Page 71, Option C that claims "An instance variable of type double defaults to 0.0" is incorrect.

Which of the following are correct? (Choose all that apply.)
A. An instance variable of type float defaults to 0.
B. An instance variable of type char defaults to null.
C. An instance variable of type double defaults to 0.0.
D. An instance variable of type int defaults to null.
E. An instance variable of type String defaults to null.
F. An instance variable of type String defaults to the empty string "".
G. None of the above

 
Marshal
Posts: 80507
455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
The d in a double literal is redundant, and 0.0 is an expression with type double by default, so there is no error in the book. It is only necessary to write d if it is needed to disambiguate the literal from an integer (e.g. 0d), but most people write 0.0.
 
Miles Jiang
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply, Campbell. But for the review question immediately following Question #6, something interesting happens.

7. Which of the following are correct? (Choose all that apply.)
A. A local variable of type boolean defaults to null.
B. A local variable of type float defaults to 0.0f.
C. A local variable of type double defaults to 0.
D. A local variable of type Object defaults to null.
E. A local variable of type boolean defaults to false.
F. A local variable of type float defaults to 0.0.
G. None of the above



Local variables do not get assigned default values, so the correct answer is Option G (tricky!). But the explanation gives another scenario:

If this question were about instance variables, options B, D, and E would be correct. A boolean primitive defaults to false, and a float primitive defaults to 0.0f.



Since Option B is correct, Option F is incorrect, as the two options are opposed to each other.

So in my opinion, strictly speaking, an instance variable or class variable of type float defaults to 0.0f and prints to 0.0. The same is true for type double.
 
Enthuware Software Support
Posts: 4906
60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you talk about values then 0.0d, 0.0f, and 0 are all zeros and are all the same. The following lines prove it:

           System.out.println(0.0f == 0.0d);
           System.out.println(0 == 0.0d);  

You need the d and f suffix only while writing those values in code and that too when assigning a floating point value to a float to make the compiler happy. The compiler wants to make sure that you know what you are doing when you do something like float f = 1.2; It won't allow this assignment because it is not sure that you know that this assignment might cause the some information to be lost (float is smaller than a double). So, it wants the f suffix in the value. To know more about this, just google how Java handles floating point numbers (not necessary for the OCA/OCP exams).

There is no particular necessity for f to be added to 0.0 while assigning it to a float variable but, I guess, it is required only for the sake of consistency.
 
Miles Jiang
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good explanation, Paul. But the point is if test takers encounter two options in the actually exam that are opposed to each other, like the example above, which one should they choose?

Say, one option claims that "an instance variable of type float defaults to 0.0f", while the other one claims that "an instance variable of type float defaults to 0.0", definitely the former one is the correct answer.

But if one option claims that "an instance variable of type float defaults to 0", while the other one claims that "an instance variable of type float defaults to 0.0", then the latter one should be the correct answer.

But if there is another option "neither is correct", should test takers choose this one instead of the option claiming "an instance variable of type float defaults to 0.0"? Interesting.

A recap for new learners regarding to assignment for double and float variables.

 
Paul Anilprem
Enthuware Software Support
Posts: 4906
60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Miles Jiang wrote:Good explanation, Paul. But the point is if test takers encounter two options in the actually exam that are opposed to each other, like the example above, which one should they choose?


Don't worry, you won't get such a question and option. What you will get is code that will either compile or fail to compile. E.g.,
float f = 0.0;

If you do get such a question where you have to select among 0, 0.0, and Neither, for a float, I would pick 0. But again, you won't get it
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic