Java Reference
In-Depth Information
When you run this program, you will see the following output:
This program introduces several new concepts. First, the statement
declares a variable called
var1
of type integer. In Java, all variables must be declared before
they are used. Further, the type of values that the variable can hold must also be specified.
This is called the
type
of the variable. In this case,
var1
can hold integer values. These are
whole number values. In Java, to declare a variable to be of type integer, precede its name
with the keyword
int
. Thus, the preceding statement declares a variable called
var1
of type
int
.
The next line declares a second variable called
var2
:
Notice that this line uses the same format as the first line except that the name of the vari-
able is different.
In general, to declare a variable you will use a statement like this:
type var-name;
Here,
type
specifies the type of variable being declared, and
var-name
is the name of the
variable. In addition to
int
, Java supports several other data types.