4.5.4
Initial Values of Variables
TYPES, VALUES, AND VARIABLES
The following example contains several different kinds of variables:
class Point {
static int numPoints;
// numPoints
is a class variable
int x, y;
// x
and
y
are instance variables
int[] w = new int[10];
// w[0]
is an array component
int setX(int x) {
// x
is a method parameter
int oldx = this.x;
// oldx
is a local variable
this.x = x;
return oldx;
}
}
4.5.4 Initial Values of Variables
Every variable in a Java program must have a value before its value is used:
Each class variable, instance variable, or array component is initialized with a
default value
when it is created ( 15.8, 15.9, 20.3.6):
N
For type
byte
, the default value is zero, that is, the value of
(byte)0
.
N
For type
short
, the default value is zero, that is, the value of
(short)0
.
N
For type
int
, the default value is zero, that is,
0
.
N
For type
long
, the default value is zero, that is,
0L
.
N
For type
float
, the default value is positive zero, that is,
0.0f
.
N
For type
double
, the default value is positive zero, that is,
0.0d
.
N
For type
char
, the default value is the null character, that is,
\u0000
.
N
For type
boolean
, the default value is
false
.
N
For all reference types ( 4.3), the default value is
null
.
Each method parameter ( 8.4.1) is initialized to the corresponding argument
value provided by the invoker of the method ( 15.11).
Each constructor parameter ( 8.6.1) is initialized to the corresponding argu
ment value provided by a class instance creation expression ( 15.8) or explicit
constructor invocation ( 8.6.5).
An exception handler parameter ( 14.18) is initialized to the thrown object
representing the exception ( 11.3, 14.16).
A local variable ( 14.3, 14.12) must be explicitly given a value before it is
used, by either initialization ( 14.3) or assignment ( 15.25), in a way that can
be verified by the compiler using the rules for definite assignment ( 16).
46
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved