Java Reference
In-Depth Information
library. Look for the first line in your code that appears in the exception report. For
example, skip the line that refers to
java.lang.String.substring(String.java:1444)
The next line in our example mentions a line number in your code,
Homework1.java . Once you have the line number in your code, open up the
file, go to that line, and look at it! In the great majority of cases, knowing the name
of the exception and the line that caused it make it completely obvious what went
wrong, and you can easily fix your error.
A DVANCED T OPIC 4.4: Escape Sequences
Suppose you want to display a string containing quotation marks, such as
Hello, "World"!
You can't use
System.out.println("Hello, "World"!");
As soon as the compiler reads ÐHello, Ñ , it thinks the string is finished, and
then it gets all confused about World followed by two quotation marks. A human
would probably realize that the second and third quotation marks were supposed to
be part of the string, but a compiler has a one-track mind. If a simple analysis of
the input doesn't make sense to it, it just refuses to go on, and reports an error.
Well, how do you then display quotation marks on the screen? You precede the
quotation marks inside the string with a backslash character. Inside a string, the
sequence \ȓ denotes a literal quote, not the end of a string. The correct display
statement is, therefore
System.out.println("Hello, \"World\"!");
The backslash character is used as an escape character; the character sequence \ȓ is
called an escape sequence. The backslash does not denote itself; instead, it is used
to encode other characters that would otherwise be difficult to include in a string.
Now, what do you do if you actually want to print a backslash (for example, to
specify a Windows file name)? You must enter two \ in a row, like this:
Search WWH ::




Custom Search