Java Reference
In-Depth Information
is conceivable that the int could be boxed into an Integer type and the second
method1() used. That might even be what you want to happen - it might make
more sense to convert an int to an Integer than to promote it to a long .
While arguably reasonable, that is not what happens. The general rule is, for
compatibility reasons, the same behavior that applied in pre-5.0 versions must
continue to hold. The reason is that existing code cannot suddenly start behaving
differently when compiled and run under 5.0.
We would suggest that an even better rule is to realize that using overloads
like this is confusing and potentially asking for trouble. There is little reason to
write such obfuscated code.
3.8 Arrays
An array provides an ordered, sequential collection of elements. These elements
consist of either primitive values or object references. Here we concentrate on
primitive array types. We discuss arrays in more detail in the next chapter. How-
ever, arrays are very useful for the demonstration programs and exercises, so we
learn enough with this brief introduction to implement arrays of primitive type
values.
Yo u can declare a one-dimensional array of primitive type values in two ways:
int iArray[];
float[] fArray;
Yo u can put the brackets after the array name as shown in the first line or after
the type declaration as shown in the second line. As a matter of style, the second
method is preferred; you are creating a float array, so the array symbols go
with the float keyword.
Yo u create an array of a given size and with default values for the elements
using the new operator and putting the array size in brackets. For example,
int[] iArray = new int[10];
creates an int array that is ten elements long. Here are some other examples:
long[] lArray = new long[20];
int n = 15;
short[] sArray = new short[n];
Forarraysofprimitive type values, the declaration creates the array object (Java
arrays are objects) and allocates memory for each primitive element and sets each
element to a default value for that type. For numeric types, the default values equal
zero (0 for integers, 0.0 for floating-point). For boolean arrays the default value
for each element is false .For char the default is the Unicode value “
\
u0000”
(all bits equal 0).
The size of the array in the declaration must be an int integer, so arrays are
limited to the maximum value of an int ,which equals 2 147 483 647.
Search WWH ::




Custom Search