Java Reference
In-Depth Information
Yo u can also create and initialize an array to particular values in the declaration
by putting the elements between braces and separated by commas, as in
int[] iArray ={1, 3, 5, 6 } ;
long[] lArray ={10, 5, 3 } ;
double[] dArray ={1.0, 343.33, -13.1 } ;
Here the compiler counts the number of elements provided and automatically
sizes the array to that number. Array elements are accessed with an int value
inside brackets with a value between 0 and n-1 ,where n equals the size of the
array. For example,
int ii = iArray[3] * 5;
double d = dArray[0] / 3.2;
Yo u can find the size of an array via the length property:
int arraySize = iArray.length;
Note again that the above declarations work only for arrays of primitive type
values. For arrays of objects, an object must be created separately for each element
in the array. In other words, the array is really just a list of references to other
objects that must be created in a separate step. We discuss arrays of objects in the
next chapter.
3.9 Exceptions
If a Java program attempts to carry out an illegal operation, the JVM does not
necessarily halt processing at that point. In most cases, the JVM allows for the
possibility of detecting the problem and recovering from it. To emphasize that
such problems are not fatal, the term exception is used rather than error.
Forexample, what if by accident we put a non-numeric string into an applet
tag parameter and it is passed to the wrapper method Integer.parseInt()
as shown below:
class MyApplet extends Applet
{
...
init () {
// If the parameter returned is not
// numeric, there will be a problem:
int data = Integer.parseInt (getParameter ("intNumber"));
aMethod (data);
}
}
 
Search WWH ::




Custom Search