|
| |
Common Syntax Errors in Java
(compile time errors)
The following list describes some errors that are easy to make in
writing Java ...
- class name does not match file name
(usually this is due to uppercase vs lower case mistake or simple typo)
- misspelled variable name
(use of variable name does not match name in its declaration)
- missing semicolon after assignment or method call statement
- missing semicolon after variable definition
- missing semicolon after import statement
- import statement naming package not classes
- giving semicolon after method signature
- missing parenthesis "(" and
")" around condition in if
or while statement
- missing parentheses "(" and
")" in method call without
arguments
- missing the variable type for an argument in the parameter list of method
declaration
- supplying the variable type for an argument in the parameter list of
method call
- redefining the type of a variable (defining a variable which is
already defined)
- missing return statement at end of some unreachable path in code
- declaring method as static when it mentions instance variables
- confusing numeric char '2' with int 2
- confusing one character string "x" with character 'x'
The Most Common Syntax Errors in Java:
1. Spelling Mistakes
Simple spelling errors
The Java language is a strict language. A variable or method CAN NOT be used
before it is declared and a common mistake is to misspell the variable/method
name when using it
Here are some general rules for naming variables in java:
- Java variable names are case sensitive.
- Java names start with any letter or underscore (_) or dollar sign ($).
- Java names can be of any length up to 64 characters
Case-sensitive errors with classes
The Java language is Case Sensitive! For example, Java will not
recognize the word string as a valid type in the
language as you should have written String.
Java will generate an error message of the form ...
Line nn: Class xxxx not found in type declaration.
... where xxxx is the name of the class which has not been given the correct
capitalization.
Case-sensitive errors with variables and Methods
The Java language is Case Sensitive! It is a common mistake to
miss the fact that variables are case sensitive. For example, you may have
declared the variable Name as an String and then later on in
your program you try to refer to the variable name.
This gives rise to error messages of the form ...
Line nn: Undefined variable: xxxx
... where xxxx is the name of the variable which has been mistyped.
Capitalization of Java key words
The Java language is Case Sensitive! So that the method System.out.print()
is different from the method system.out.print() and
the method main() is different than the method Main().
The Java compiler will issue an error message such as ...
Line nn: class or interface declaration expected
... when it does not recognize a variable, method or Java Object because it
is misspelled (including its case)
2. Bracketing Mistakes
Missing } brackets
This is a common programming error in many programming languages and can be
fixed by adding the closing bracket. All blocks of code must be started with a
{ bracket and ended/closed with a }bracket.
This type of error can be reduced by using a good code indentation system.
Missing ) brackets
This is a common programming error in many programming languages and can be
fixed by adding the closing bracket. Methods must use the ( and ) to surround
their parameter list.
Improper bracket use
This is a common programming error for beginning programmers. It is important
to know when to use the different types of bracket.
| { } |
Used for blocks of code - like loops, if statements,
classes and method |
| ( ) |
Used in method headers to surround the parameter list
Use in Math equations |
| [ ] |
Used in arrays to denote the array index position |
Improper Nesting of { } brackets
This is a common programming error for beginning programmers. Each block of
code surround by the { and } brackets must be properly structured so that two
blocks of code are not straggled but rather one block of code must be
contained inside of another block of code.
This type of error can be reduced by using a good code indentation
system.
Missing brackets in a no-argument Method
When you use a method which has no arguments you should place brackets after
the name of the method.
For example, if you have declared a method PrintBlankScreen with
no arguments then you should code this as:
PrintBlankScreen( )
rather than:
PrintBlankScreen
The compiler will usually indicate an error message of the form:
Line nn: Invalid expression statement
3. Improper Declaration/Use of Methods
Writing the wrong format for a class method
Class methods have the form:ClassName.MethodName(Argument(s))
A common error is to forget the class name. If you do, then you will get an
error message of the form:Line nn: '}' expected
Specifying method arguments wrongly
When you define classes you should prefix each argument with the name of a
scalar type or the name of an existing class. For example:
public void tryIt(int a, int b, URL c)
A common error that programmers from other languages make is to forget to
prefix every argument with its type. For example, an erroneous version
of the definition above would be:
public void tryIt(int a, b URL c)
This type of error will give rise to error messages of the form:
Line nn: Identifier expected
Omitting void in methods
When a method returns no result, but just carries out some action, you need to
use the keyword void in front of the name of the method. If you do not use
this keyword, then it will give rise to error messages of the form:Line nn: Invalid method declaration; return type required
Omitting the return in a method
When a method returns a value, then the body of the method should include at
least one return statement which returns the right type of value.
Failing to include a return statemnet will generate an error message of the
form ...
Line nn: Return required at end of xxxx
... where xxxx is the method which does not contain the return.
Treating a static method as if it were an instance method
Static methods are associated with messages sent to classes rather than
objects. A common error is to send static method messages to objects. For
example, in order to calculate the absolute value of an int value and
place it into the int variable you should write:int result = Math.abs(value);
rather than:int result = value.abs();
This gives rise to a variety of syntax errors. The most common one is of the
form:Line nn: Method yyyy not found in class xxxx.
where yyyy is the name of the method and xxxx is the name of the
class within which it is called.
4. Forgetting to import a package
This one of the most common errors that inexperienced Java programmers make.
If you forget to put the required import statement at the beginning
of a program, then the compiler will respond with a message such as ...
Line nn: Class xxxx not found in type declaration
Don't forget, though, that java.lang is imported automatically and,
hence, does not need an import statement.
Links:
Source: TVDSB. |