■ The default type for floating point numbers is double.
float f = 3.4; needs a cast.
float f = 3.4f; works.
float f = ( float ) 3.4; works.
float f = 3; works without a cast. See widening rules
■ Class-level variables get initialized with their respective defaults. Method-level or block-level variables don’t ever get initialized. The code below demonstrates.
public
class Initializer {
int a; //
member variable a is initialized
String b; //
member variable b is initialized
public Initializer( ) {
char c; // c is not initialized - it's in a constructor
}
public static void main(String[ ] args) {
double d; // d is not initialized - it's in a method
}
public void amethod( ) {
Float e; // e is not initialized - it's in a method
}
}
■ An exception to the previous rule is arrays. Arrays always do get initialized, no matter where they reside - at the class level, method level, or block level. i.e.
public
class ArrayInitializer {
int a[ ] = new int[ 3 ]; //
a is initialized with zeroes
String b[ ] = new String[ 3 ]; // b is
initialized with nulls
public ArrayInitializer() {
char c[ ] = new char[ 3 ]; //
c is initialized with zeroes
}
.
. . .
public static void main(String[ ] args) {
double d[ ] = new double[ 3 ]; // d is initialized
with zeroes
}
public void amethod() {
Float e[ ] = new Float[ 3 ]; // e is initialized with
nulls
}
}
■ Creation defaults are:
numerics.get 0 or 0.0
booleans get false
object reference variables and wrappers get null
arrays of primitives get zeroes.
arrays of wrappers (which are objects!) get nulls as defaults, not zeroes or falses.
arrays of Booleans get nulls.
arrays of booleans gets falses like regular booleans.
■ The default visibility modifier is “in
the same package.”