Java Reference
In-Depth Information
// Direct assignment from floating point to double is fine, but you need a
// cast to go from double to float:
biggerNumber = bigNumber;
// OK
bigNumber = biggerNumber;
// an error
bigNumber = (float) biggerNumber;
// OK
A RRAYS
Arrays in Java are very similar to arrays in COBOL. Arrays are objects that contain
a set of other objects or a set of data elements. All the elements in an array must be
of the same type. In addition, the size of an array must be defined when it is created
and cannot be dynamically adjusted in size. These are all specifications that should
be familiar to the COBOL programmer.
01 ERROR-MESSAGE-ITEMS.
03 ERROR-MESSAGE OCCURS 10 TIMES PIC X(80).
// In Java this would be:
ErrorMsg myErrorMsgs[] = new ErrorMsg[10];
// or
int errorNumbers[] = new int [10];
Note that in the first example, the myErrorMsgs array was created, but the
ErrorMsg objects that it will eventually contain were not created. Instead, the object
reference variables inside the array are initialized to null . In contrast, an array of
data items that contains numeric data items are initialized to 0 by default.
The array brackets can be placed by the type name instead of the variable name.
This actually is the more common syntax:
int[] errorNumbers = new int [10];
Interestingly, it is possible to define an array with no more information than
the types of elements it will contain:
ErrorMsg[] myErrorMsgs;
Search WWH ::




Custom Search