Java Reference
In-Depth Information
S ELF C HECK
15. Suppose you omit the // characters from the HelloPrinter.java
program but not the remainder of the comment. Will you get a
compile-time error or a runtime error?
16. How can you find logic errors in a program?
C OMMON E RROR 1.2: Misspelling Words
If you accidentally misspell a word, then strange things may happen, and it may not
always be completely obvious from the error messages what went wrong. Here is a
good example of how simple spelling errors can cause trouble:
public class HelloPrinter
{
public static void Main(String[] args)
{
System.out.println("Hello, World!");
}
}
24
25
This class defines a method called Main . The compiler will not consider this to be
the same as the main method, because Main starts with an uppercase letter and the
Java language is case sensitive. Upper- and lowercase letters are considered to be
completely different from each other, and to the compiler Main is no better match
for main than rain . The compiler will cheerfully compile your Main method, but
when the Java virtual machine reads the compiled file, it will complain about the
missing main method and refuse to run the program. Of course, the message
Ȓmissing main methodȓ should give you a clue where to look for the error.
If you get an error message that seems to indicate that the compiler is on the wrong
track, it is a good idea to check for spelling and capitalization. All Java keywords
use only lowercase letters. Names of classes usually start with an uppercase letter,
names of methods and variables with a lowercase letter. If you misspell the name of
a symbol (for example, ouch instead of out ), the compiler will complain about an
Ȓundefined symbolȓ. That error message is usually a good clue that you made a
spelling error.
Search WWH ::




Custom Search