int
array called
n
. Then,
this array is cycled through from back to front. In the process, each integer value in
n
is
used as an index into
digits
, with the corresponding string being displayed.
Strings Are Immutable
Here is something that might surprise you: The contents of a
string
object are immutable.
That is, once created, the character sequence comprising that string cannot be altered. This
restriction allows strings to be implemented more efficiently. Even though this probably
sounds like a serious drawback, it isn’t. When you need a string that is a variation on one
that already exists, simply create a new string that contains the desired changes. Since
unused string objects are automatically garbage-collected, you don’t even need to worry
about what happens to the discarded strings.
It must be made clear, however, that
string
reference variables may, of course, change
which object they refer to. It is just that the contents of a specific
string
object cannot be
changed after it is created.
To fully understand why immutable strings are not a hindrance, we will use another of
Do'stlaringiz bilan baham: |