Java Reference
In-Depth Information
Sample Run: (In this sample run, the user input is shaded.)
Enter the number of star lines (1 to 20) to be printed: 10
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
In the method main , the user is first asked to specify how many lines of stars to print. (In
this program, the user is restricted to 20 lines because a triangular grid of up to 20 lines fits
nicely on the screen.) Because the program is restricted to only 20 lines, the while loop
in the method main ensures that the program prints the triangular grid of stars only if the
number of lines is between 1 and 20.
7
Primitive Data Type Variables as Parameters
In Chapter 3, you learned that Java has two categories of variables—primitive type
variables and reference variables. Before considering examples of void methods with
parameters, let's make the following observation about variables of primitive types
and reference variables. When a method is called, the value of the actual parameter is
copied into the corresponding formal parameter. If a formal parameter is a variable of
a primitive data type, then after copying the value of the actual parameter, there is no
connection between the formal parameter and the actual parameter. That is, the
formal parameter has its own copy of the data. Therefore, during program execution,
the formal parameter manipulates the data stored in its own memory space. The
program in Example 7-8 further illustrates how a formal parameter of a primitive data
type works.
EXAMPLE 7-8
//Example 7-8
//Program illustrating how a formal parameter of a
//primitive data type works.
public class PrimitiveTypeParameters
//Line 1
{
//Line 2
public static void main(String[] args)
//Line 3
 
Search WWH ::




Custom Search