Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

K&B book chapter 1 Q.No.15

 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question is
Which four describe the correct default values for array elements of the types indicated?
a)int =>0
b) String =>"null"
c) Dog => null
d)char =>'\u0000'
e)float => 0.0f
f) boolean => true
The answers given are a,c,d,e
I have a question regarding the option e)
Arrays are always initialised with default values and float-point variables are defined as double by default..Is e) a right choice?
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An array of floats can only hold float type compatible elements (double is larger). The default value of any element is of type float.
Refer JLS:


4.5.1 Variables of Primitive Type
A variable of a primitive type always holds a value of that exact primitive type.



4.5.3 Kinds of Variables
Array components are unnamed variables that are created and initialized to
default values (�4.5.5) whenever a new object that is an array is created
(�15.10). The array components effectively cease to exist when the array is no
longer referenced. See �10 for a description of arrays.


[ November 03, 2003: Message edited by: Vad Fogel ]
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arrays are always initialised with default values and float-point variables are defined as double by default..Is e) a right choice?[/QB]


I think what you are referring to is assigning a floating point literal to a variable. If you don't specify that is is a float by appending an 'f' it will be a double by default. When an array is initialized, however, if it is to contain floats, then the JVM will insert the default *float* value (not the default floating-point value).
 
Lakshmi Saradha
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you..
 
Paper jam tastes about as you would expect. Try some on this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic