Re: Default value of basic variable type



Macneed wrote:
i am a newbie,
i remember i read a book talking about when u declare a array variable using
float[] ABC = new float[10];

the whole array element in ABC ( ABC[0] to ABC[9] ) will automatic initialize to 0
and all type of numeric variable is initialize to 0, bool type is false,

is it true?

In an instance of a class, yes. So all the floats in a "new float[10]" get initialized to the default value for the float (0). Likewise, all the bools in a "new bool[10]" get initialized to false.

Note, however, that a stack variable is not initialized. You're required to initialize it explicitly before you use it. The compiler generates an error if you attempt to use the variable before you initialize it (and in many cases, even if you do, if you're not clear enough to suit the compiler :) ).

Pete
.



Relevant Pages

  • Re: Setting pointer to null!
    ... I think this debugging fetaure emerged originally ... developer forgets to initialize a variable and it accidentally ... to declare a bunch that would be "initialized" in relatively distant code. ... because you prevent the compiler from initializing them ...
    (microsoft.public.vc.language)
  • Re: abstract sub programs overriding
    ... >>compiler can determine that Object is never referenced before it is ... >>implicit Finalize. ... >>the Initialize is user defined. ... > if that initial value is an aggregate, it has to be built directly in the ...
    (comp.lang.ada)
  • Re: compiler bugs
    ... In a language like C, the behavior of programs that reference ... Not if the unoptimizing compiler initializes all variables to some ... portion of the array in your algorithhm. ... that one actually does not need to initialize all variables (unlike ...
    (comp.compilers)
  • Re: Javac issues "might be uninitialized" for null test
    ... And any compiler worth its salt would ... Java does not initialize variables (as opposed to class ... Some languages "initialize with garbage" and allow ... compare the non-existent value to `null' is erroneous. ...
    (comp.lang.java.programmer)
  • Re: Replicating results
    ... gradient calculation used in a large optimization routine. ... It will make sure that if you do something like declare a variable called "x1" and use a variable called "xl" the compiler will tell you that xl was undeclared. ... Make sure that you initialize all of your variables, ...
    (comp.lang.fortran)