Java Reference
In-Depth Information
Common Programming Error
Not Closing a String Literal or Comment
Every string literal has to have an opening quote and a closing quote, but it's
easy to forget the closing quotation mark. For example, you might say:
System.out.println("Hello, world!);
This produces two different error messages, even though there is only one
underlying syntax error:
Hello.java:3: unclosed string literal
System.out.println("Hello, world!);
Hello.java:4: ')' expected
}
2 errors
In this case, the first error message is quite clear, including an arrow pointing at
the beginning of the string literal that wasn't closed. The second error message
was caused by the first. Because the string literal was not closed, the compiler
didn't notice the right parenthesis and semicolon that appear at the end of the line.
A similar problem occurs when you forget to close a multiline comment by
writing */ , as in the first line of the following program:
/* This is a bad program.
public class Bad {
public static void main(String[] args){
System.out.println("Hi there.");
}
} /* end of program */
The preceding file is not a program; it is one long comment. Because the com-
ment on the first line is not closed, the entire program is swallowed up.
Luckily, many Java editor programs color the parts of a program to help you
identify them visually. Usually, if you forget to close a string literal or comment, the
rest of your program will turn the wrong color, which can help you spot the mistake.
Java was designed for a different kind of decomposition that is more noun- or
object-oriented. Instead of thinking of the problem as a series of actions to be per-
formed, we think of it as a collection of objects that have to interact.
 
Search WWH ::




Custom Search