Download Strings

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Strings
Java Foundations:
Introduction to Programming and Data Structures
by John Lewis,
Peter DePasquale and Joseph Chase
© 2007 Pearson Addison-Wesley. All rights reserved
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Passing Parameters - Review
• Recall from IS127: primitive and Object method
parameters are passed by value, not by reference. Review p. 219
• What is the implication of this?
– It means that if we want to obtain an updated value in
the calling function (e.g., main driver) then we must
find another solution. Some possibilities are:
• return
• arrays
• In today’s lesson we will explore the issue of
updating values a little more. Today we will focus
on Strings.
• Strings are strange…
© 2007 Pearson Addison-Wesley. All rights reserved
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
2-2
Objects, Not Primitives
• Strings are objects, not primitives
• String have various methods that can be used.
Use Google to look up various methods (Java
String)
• http://java.sun.com/j2se/1.5.0/docs/api/java/lang/
String.html
• Open S1_strMethods and experiment with the
various string methods
© 2007 Pearson Addison-Wesley. All rights reserved
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
2-3
Immutability – what is it?
• Describe the significance of the immutability of
String objects
– The theory of the immutability of the String class
says that once created, a string can never be changed.
Can this be possible?
• Open S2_immutability.java to test: can a string,
which is immutable, be changed?
• If a string can be changed, then what does
immutability really mean?
© 2007 Pearson Addison-Wesley. All rights reserved
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
2-4
Immutability - example
• Let’s consider the immutability example.
– The immutability really refers to what the String
reference points to in memory. When S2 is assigned
to S1 in the example, the String containing "Hello" in
the String pool is no longer referenced and S1 now
points to the same string as S2. The fact that the
"Hello" string has not actually been modified is fairly
theoretical as you can no longer "get at it".
• Take a look at page 77 for a visual example.
© 2007 Pearson Addison-Wesley. All rights reserved
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
2-5
References
• Note that a primitive variable contains the value itself,
but an object variable contains the address of the object
• An object reference can be thought of as a pointer to the
location of the object
• Rather than dealing with arbitrary addresses, we often
depict a reference graphically
num1
38
name1
© 2007 Pearson Addison-Wesley. All rights reserved
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
"Steve Jobs"
2-6
Assignment Revisited
• The act of assignment takes a copy of a value and stores
it in a variable
• For primitive types:
Before:
num1
38
num2
96
num2 = num1;
After:
num1
38
num2
38
© 2007 Pearson Addison-Wesley. All rights reserved
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
2-7
Reference Assignment
• For object references, assignment copies the
address:
"Steve Jobs"
name1
Before:
name2
"Steve Wozniak"
name2 = name1;
name1
What??
Still
here??
After:
name2
© 2007 Pearson Addison-Wesley. All rights reserved
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
"Steve Jobs"
"Steve Wozniak"
2-8
Aliases
• Two or more references that refer to the same object are
called aliases of each other
• That creates an interesting situation: one object can be
accessed using multiple reference variables
• Aliases can be useful, but should be managed carefully
• Changing an object through one reference changes it for
all of its aliases, because there is really only one object
© 2007 Pearson Addison-Wesley. All rights reserved
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
2-9
Immutability - implications
• If strings can, for practical purposes, be changed then
what is the significance of immutability?
• Let’s try an experiment.
– Half of you will be assigned to compile S3_testString.java
– Half of you will be assigned to compile
S3_testStringBuffer.java
– DO NOT RUN the program until I tell you to.
• Read? On your mark, get set, go!
• Who won?
• Let’s step through the code and comment it to better
understand what is going on.
© 2007 Pearson Addison-Wesley. All rights reserved
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
2-10
Immutability - explanation
• Because strings are immutable, each allocation of the
String is created and never replaced.
• In order to change the value within the String, a new
String is created (remember: the original string is
immutable and can not be changed).
• Immutability allocates space and does not release it.
• This takes a lot of space…and a lot of time.
• Immutability explains why testString ran soooo much
longer – and uses up to much more memory – than
testStringBuffer.
© 2007 Pearson Addison-Wesley. All rights reserved
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
2-11
Immutability - conclusion
• Use stringBuffer instead of String when you are
going to manipulate a string.
• Even when you use String, you can be reassured
that your build of immutable strings (that are no
longer referenced or needed by the program) will
be piled into a “garbage heap” and, eventually,
Java performs “automatic garbage collection”
(see page 78).
© 2007 Pearson Addison-Wesley. All rights reserved
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
2-12
Garbage Collection
• When an object no longer has any valid references to it, it
can no longer be accessed by the program
• The object is useless, and therefore is called garbage
• Java performs automatic garbage collection periodically,
returning an object's memory to the system for future use
• In other languages, the programmer is responsible for
performing garbage collection
© 2007 Pearson Addison-Wesley. All rights reserved
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
2-13
A Final Word…
• We learned that because Strings are immutable,
StringBuffers are preferable when a lot of String
manipulation is going to happen.
• However, Strings are cool too. Strings have a lot of
valuable methods that we may want to use.
• Therefore, the final word is:
– Use StringBuffers for manipulation.
– Convert back to String when done so that you can use all the
methods.
• Open S3_testStringCombo for review.
© 2007 Pearson Addison-Wesley. All rights reserved
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
2-14
Summary
• We worked with Strings and explored
immutability.
• We compared String with StringBuffer. Based on
our speed tests, there are situations in which
StringBuffer is preferred. What are those
situations?
• StringBuffers are great. Strings are great. Use
both.
© 2007 Pearson Addison-Wesley. All rights reserved
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
2-15