Section 4 : Language Fundamentals
State the effect of using a
variable or array element of any kind when no explicit
assignment has been made to it.
All
the instance and static
variables of a class are automatically initialized to
their default values before use even if there has not
been explicit assignment to them. This is, however,
not true for local variables ( variables declared
inside a method ). As a result of this, when you try
to use a variable without initializing it first, you
get their default value. For example, an
int variable will be
initialized to zero, float
will be initialized to 0.0, a
boolean will be initialized to false and so on.
A reference variable gets initialized to null by
default. In case of a local variable, however, your
code will not compile if you do not initialize the
variable before trying to use it.
All
the elements of an array get initialized to their
default values, whether the array is an instance or
static member of a class,
or a local variable of a method. If you try to use an
array before initializing it, the compiler will not
complain. But you will get a NullPointerException at
the runtime, as the array itself will get initialized
to null value automatically. If you do initialize the
array by using new operator, you will not get
this exception at runtime. However, if you have not
initialized its elements, all you will get is the
default values of its elements.
section4-1 | section4-2 | section4-3 | section4-4
Sections :
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11
|