Every variable in a Java program must have a value before its value is used:
byte
, the default value is zero, that is, the value of (byte)0
.
short
, the default value is zero, that is, the value of (short)0
.
int
, the default value is zero, that is, 0
.
long
, the default value is zero, that is, 0L
.
float
, the default value is positive zero, that is, 0.0f
.
double
, the default value is positive zero, that is, 0.0d
.
char
, the default value is the null character, that is, '\u0000
'.
boolean
, the default value is false
.
null
.
class Point { static int npoints; int x, y; Point root; }
class Test { public static void main(String[] args) { System.out.println("npoints=" + Point.npoints); Point p = new Point(); System.out.println("p.x=" + p.x + ", p.y=" + p.y); System.out.println("p.root=" + p.root); } }
npoints=0 p.x=0, p.y=0 p.root=null
illustrating the default initialization of npoints
, which occurs when the class
Point
is prepared (§12.3.2), and the default initialization of x
, y
, and root
, which
occurs when a new Point
is instantiated. See §12 for a full description of all
aspects of loading, linking, and initialization of classes and interfaces, plus a
description of the instantiation of classes to make new class instances.