Java Reference
In-Depth Information
The following line of code assigns
var1
the value 1024:
In Java, the assignment operator is the single equal sign. It copies the value on its right side
into the variable on its left.
The next line of code outputs the value of
var1
preceded by the string "var1 contains ":
In this statement, the plus sign causes the value of
var1
to be displayed after the string that
precedes it. This approach can be generalized. Using the
+
operator, you can chain together
as many items as you want within a single
println( )
statement.
The next line of code assigns
var2
the value of
var1
divided by 2:
This line divides the value in
var1
by 2 and then stores that result in
var2
. Thus, after the
line executes,
var2
will contain the value 512. The value of
var1
will be unchanged. Like
most other computer languages, Java supports a full range of arithmetic operators, includ-
ing those shown here:
+
Addition
-
Subtraction
*
Multiplication
/
Division
Here are the next two lines in the program:
Two new things are occurring here. First, the built-in method
print( )
is used to display the
string "var2 contains var1 / 2: ". This string is
not
followed by a new line. This means that
when the next output is generated, it will start on the same line. The
print( )
method is just
like
println( )
, except that it does not output a new line after each call. Second, in the call
to
println( )
, notice that
var2
is used by itself. Both
print( )
and
println( )
can be used to
output values of any of Java's built-in types.