Java Reference
In-Depth Information
The escape sequence \n indicates the start of a new line. For example, the statement
System.out.println("To be or\nNot to be.");
will write the following to the screen:
To be or
Not to be.
You do not need to use the escape sequence \' to include a single quote inside a
quoted string. For example, "Time's up!" is a valid quoted string. However, you do
need \' if you want to indicate the constant for the single-quote character, as in
char singleQuote = '\'';
String Processing
In Java an object of type String is an immutable object , meaning that the characters
in the String object cannot be changed. This will eventually prove to be important to
us, but at this stage of our exploration of Java, it is a misleading statement. To see that
an object of type String cannot be changed, note that none of the methods in Display
1.4 changes the value of the String calling object. There are more String methods
than those shown in Display 1.4, but none of them lets you write statements that say
things like “Change the fifth character in the calling object string to 'x' ”. This was
done intentionally to make the implementation of the String class more efficient and
for other reasons that we will discuss later in this topic. There is another string class,
called StringBuffer , that has methods for altering its string object. We will not dis-
cuss the class StringBuffer in this text, but a table explaining many of the methods of
the class StringBuffer is included in Appendix 4.
Although there is no method that allows you to change the value of a String
object, such as "Hello" , you can still write programs that change the value of a String
variable, which is probably all you want anyway. To perform the change, you simply
use an assignment statement, as in the following example:
immutable
object
String name = "Soprano";
name = "Anthony " + name;
The assignment statement in the second line changes the value of the name variable so
that the string it names changes from "Soprano" to "Anthony Soprano" . Display 1.7
contains a demonstration of some simple string processing.
The Unicode Character Set
Until recently, most programming languages used the ASCII character set, which is
given in Appendix 3. The ASCII character set is simply a list of all the characters nor-
mally used on an English-language keyboard plus a few special characters. In this list,
each character has been assigned a number so that characters can be stored by storing
ASCII
Search WWH ::




Custom Search