您的位置:寻梦网首页编程乐园Java天地Core JavaJava Lecture Notes
Introduction
Content
Apply
Reflect
Extend
previous.gif
 (3087 bytes)
next.gif
 (2959 bytes)

 
 
Content Index
Content Page # 8

Common declaration and definition errors

Two common errors (though easily spotted and debugged) as:

  • having a different type when declaring and defining an array
  • forgetting the double square brackets "[" "]" when declaring or defining an array
The of these problems is illustrated with the example applet ArrayDefinition below: import java.applet.Applet;

class ArrayDefinition extends Applet
{
    double monthlySales[];

    public void init()
    {

  monthlySales = new int[12];     }
}
When compiled, this applet results in the following output:
The error in this case is that the array monthlySales has been declared as an array of type double, but when its dimension (size ) 12 is defined, the variable type int has been written by mistake.
The second common error, forgetting the open and closed square brackets is illustrated in the program below:
import java.applet.Applet;

class ArrayDeclaration extends Applet
{
    double dailyCosts;

    public void init()
    {

  dailyCosts = new double[31];     }
}
The result of trying to compile this program is as follows:
The error in this case is that when declared, the programmer forget to include the double square brackets. The line:
double dailyCosts;
is valid, but does not declare dailyCosts to be an identifier of an array of double variables ?it simply declares dailyCosts to be the identifier of a one double variable.
 

Back to top

basicline.gif (169 bytes)

RITSEC - Global Campus
Copyright © 1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg