Java Reference
In-Depth Information
zero-equivalent for all reference types is the special value
null
, which indicates “no
object”:
[0]
[1]
[2]
points
null
3
null
3
null
3
The actual
Point
objects must be constructed separately with the
new
keyword, as
in the following code:
Point[] points = new Point[3];
points[0] = new Point(3, 7);
points[1] = new Point(4, 5);
points[2] = new Point(6, 2);
After these lines of code execute, your program will have created individual
Point
objects referred to by the various array elements:
[0]
[1]
[2]
points
xx3 y7
x
x3
4
y5
x
6
y2
Notice that the
new
keyword is required in four different places, because there are
four objects to be constructed: the array itself and the three individual
Point
objects.
You could also use the curly brace notation for initializing the array, in which case
you don't need the
new
keyword to construct the array itself:
Point[] points = {new Point(3, 7), new Point(4, 5), new Point(6, 2)};
As you've seen since Chapter 1, whenever you define a
main
method, you're
required to include as its parameter
String[] args
, which is an array of
String
objects. Java itself initializes this array if the user provides what are known as
command-line arguments
when invoking Java. For example, the user could exe-
cute a Java class called
DoSomething
from a command prompt or terminal by
using a command like:
java DoSomething
The user has the option to type extra arguments, as in the following:
java DoSomething temperature.dat temperature.out
Search WWH ::
Custom Search