1149-1152

Previous Table of Contents Next

Page 1149

 long valsC[][] = new long[5][2]; float valsD[][] = new float[5][]; double valsE[][][] = new double[5][3][2]; char valsF[][][] = new char[10][][]; 

Note that array indexes are zero based and that the new operator is used to allocate memory for a dimension. Only the leftmost dimension must be allocated for multidimensional arrays. The following code fragment demonstrates some of the flexibility of Java arrays:

 int Array = new int[3][]; Array[0][] = new int[3]; Array[1][] = new int[5]; Array[2][] = new int[10]; 

Java does not support pointers, so basic datatypes always are passed by value as method parameters. Objects, on the other hand, always are passed by reference. Java defines object equivalents for the basic datatypes (integer, Boolean, and so on) so that the types can be passed by reference and supplied to methods that accept a generic Object reference (Object is the base for many Java classes). The Java String class is a special case that has no true basic type equivalent.

Java defines full sets of mathematical, relational, logical, and bitwise operators. Table 52.2 lists the most commonly used operators. The bitwise operators rarely are used outside the context of developing very low-level libraries, so they have been omitted from this overview.

Table 52.2. Commonly used Java operators.


Operator Symbol Operator Name
Mathematical Operators
+ Addition
+= Addition assignment
_ Subtraction
_= Subtraction assignment
* Multiplication
*= Multiplication assignment
/ Division
/= Division assignment
% Modulus
%= Modulus assignment
++ Increment
__ Decrement
 continues 

Page 1150

Table 52.2. continued


Operator Symbol Operator Name
Relational Operators
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
Logical Operators
& Logical AND
&= AND assignment
Logical OR
= OR assignment
^ Exclusive OR
^= Exclusive OR assignment
Short-circuit OR
== Equal to
&& Short-circuit AND
!= Not equal to
! Logical NOT
?: Immediate if

Java's operators are based on the C language. Java control statements and syntax also will be familiar to you if you are a C programmer.

Java provides branching and looping mechanisms through statements that are common to many third-generation languages. The basic control statements include the following:

  • if-else
  • switch
  • for
  • while
  • do-while

The if and switch statements are used for conditional branching. The switch statement is less flexible because it operates on a single expression, but it can be used instead of multiple if-else statements in some cases. The following fragment, for example, illustrates how to implement multiple if-else statements that are equivalent to a single switch statement:

Page 1151

 // this is a comment _ the fragment below demonstrates the use of the if statement if (theval == 1) {     do_this(); } else if (theval == 2) {     do_that(); } else {     do_other(); } continue_processing(); /* this is a multi-line comment     the fragment below demonstrates the use of the switch statement */ switch (theval) {     case 1:         do_this();         break;     case 2:         do_that();         break;     default:         do_other(); } continue_processing(); 

The break statement is used to exit an enclosing execution block. When break is encountered in the switch example, execution jumps to continue_processing().

The for, while, and do-while statements are used for iteration. The following code fragment demonstrates the use of these statements to produce equivalent results:

 for (n = 0; n < 5; n++) {     // do something useful } n = 0; while (n < 5) {     // do something useful     n++; } n = 0; do {     // do something useful     n++; while (n <= 5) 

You can use the break statement to jump out of a loop. You can use the continue statement to skip to the next evaluation, as demonstrated in this example:

 for (n = 0; n < 5; n++) {     // do something useful     if (we_want_to_stop)         break;     if (we_want_to_skip_other_things)         continue;     other_things(); } 

Page 1152

If the expression we_want_to_skip_other_things evaluates to true, execution resumes at the top of the loop.

Java also supports structured exception handling through the use of try and catch. Many of the methods an application invokes have the potential of throwing an exception or an object that extends Exception, which is a class defined in the Java Development Kit (JDK) core API. Any method that declares a throws clause must be enclosed in a try block. The core API defines many exceptions, and there are cases in which using the basic datatypes and language constructs can cause an exception to be thrown, as shown in this example:

 try {     int vals[] = new int[4];     vals[4] = 0; } catch (ArrayIndexOutOfBoundsException e) {     System.out.println(e.getMessage()); } 

This example catches a specific exception and invokes methods to print any text provided by the ArrayIndexOutOfBoundsException to standard output. The same exception can be caught by this statement:

 catch (Exception e) 

Exception is the base (or superclass) of the more specific exception, so it can be used as a catch-all. On the other hand, you can use multiple catch statements to provide specific handlers for a single try block. When you provide multiple handlers, you must order them from lowest to highest in the hierarchy. In the following fragment, for example, the second catch is never reached:

 try {     // do a bunch of things } catch (Exception e1) {     // do something } catch (ArrayIndexOutOfBoundsException e) {     // we will never get here } 

ArrayIndexOutOfBoundsException inherits (indirectly) from Exception, so it is caught in the first block. You can define application-specific exceptions by extending Exception. Exceptions can be thrown only from methods that define them in their throws clause, such as in this example:

 public int do_something() throws Exception 

Exception can be thrown from within the method by using this statement:

 throw new Exception("PANIC! SOMETHING REALLY BAD HAPPENED!"); 

You can construct an exception with a text message that can be retrieved when it is caught.

Previous Table of Contents Next


Oracle Unleashed
Oracle Development Unleashed (3rd Edition)
ISBN: 0672315750
EAN: 2147483647
Year: 1997
Pages: 391

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net