Java Reference
In-Depth Information
float [][][] globalTemperatureData = new float [ 360 ][][ 100 ]; // Error!
float [][][] globalTemperatureData = new float [][ 180 ][ 100 ]; // Error!
Like a one-dimensional array, a multidimensional array can be initialized using an
array initializer. Simply use nested sets of curly braces to nest arrays within arrays.
For example, we can declare, create, and initialize a 5 × 5 multiplication table like
this:
int [][] products = { { 0 , 0 , 0 , 0 , 0 },
{ 0 , 1 , 2 , 3 , 4 },
{ 0 , 2 , 4 , 6 , 8 },
{ 0 , 3 , 6 , 9 , 12 },
{ 0 , 4 , 8 , 12 , 16 } };
Or, if you want to use a multidimensional array without declaring a variable, you
can use the anonymous initializer syntax:
boolean response = bilingualQuestion ( question , new String [][] {
{ "Yes" , "No" },
{ "Oui" , "Non" }});
When you create a multidimensional array using the new keyword, it is usually good
practice to only use rectangular arrays: one in which all the array values for a given
dimension have the same size.
Reference Types
Now that we've covered arrays and introduced classes and objects, we can turn to a
more general description of reference types . Classes and arrays are two of Java's five
kinds of reference types. Classes were introduced earlier and are covered in com‐
plete detail, along with interfaces , in Chapter 3 . Enumerated types and annotation
types are reference types introduced in Chapter 4 .
This section does not cover specific syntax for any particular reference type, but
instead explains the general behavior of reference types and illustrates how they dif‐
fer from Java's primitive types. In this section, the term object refers to a value or
instance of any reference type, including arrays.
Reference Versus Primitive Types
Reference types and objects differ substantially from primitive types and their prim‐
itive values:
• Eight primitive types are defined by the Java language, and the programmer
cannot define new primitive types. Reference types are user-defined, so there is
an unlimited number of them. For example, a program might define a class
named Point and use objects of this newly defined type to store and manipu‐
late x,y points in a Cartesian coordinate system.
• Primitive types represent single values. Reference types are aggregate types that
hold zero or more primitive values or objects. Our hypothetical Point class, for
Search WWH ::




Custom Search