Download 5 String Class

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
String Class
Variable
Holds a primitive value or a reference to
an object.
 A reference lets us know where the object
is located in memory.

reference
Object
Declaring an object
String name;
Class
variable
In Memory:
name
Creating an Object
name = new String (“Louise”);
new
operator
className
actual
parameter
“Louise” is a String Literal
 This is instantiation of an object.
In memory:
String Object

name
“Louise”
name is an instance of the String Class
 new operator calls a constructor which is
a method that has the same name as the
class.
 Constructor: used to set the field data of
an object. Gives its initial set up.
 In this example, the constructor is passed
a string literal that specifies the characters
that the String object will hold.
 After an object is instantiated we use the
dot operator to access its methods

char charAt(int index)
Return
type
method
name
formal
parameter
char c;
c = name.charAt(3);
indexes
0
1
2
3
4
5
L
o
u
i
s
e
indexes
Indexes always start at 0 and go to
length() – 1.
 Arrays, ArrayList, and 2D ArrayLists are
labeled the same way.
 So, for String name = “Louise”;, what is the
index of L? e?
 How can we find out what the first
character of name is?

Exercise
Write a series of statements which declares
and creates a String which will hold your
school name and a char variable to hold
the 2nd letter in the school name.
String school = new String (“Lakeside”);
Or
String school = “Lakeside”;
char secondChar = school.charAt(1);
Remember when ever a String literal appears, Java automatically
creates an object.
int compareTo (String str)
return
type
method
name
Formal
parameter
In the interactions pane declare and create a
String named word that is assigned to the String
“book”. Use compareTo to compare it with
“took”, “Book”, and “bark”
If the string comes before our word alphabetically
we get a positive return value, if it comes after
our word, we get a negative. If the words are
exactly the same, we get 0.
Uppercase comes before lowercase in unicodes.
Short Cut to String Creation
Because the String class is used so often,
Java allows it to act like a primitive data.
But ALWAYS REMEMBER IT IS AN
OBJECT!!
 We can create Strings without using new.
String name = “Louise”;


Whenever a String literal appears, Java
creates a String object.
String concat (String str)
What is the result:
book
String word = new String (“book”);
word = word.concat (“store”);
We could also do…
word = word + “store”;
Or
word += “store”;
1st
word
2nd
bookstore
Remember Strings are immutable – they can not be changed, so when we
concatenate book and store, we are actually creating a new String object
“bookstore” and having word reference it instead. Since the original String
“book” has no reference, Java does automatic garbage collection and
removes it from memory.
boolean equals (String str)
boolean tf;
tf = word.equals (“BOOKSTORE”);
tf = word.equals (“bookstore”);
Try these in DrJava’s interaction pane, and
record the results. What does equals do?
boolean equalsIgnoreCase(String str)
boolean tf;
tf = word.equalsIgnoreCase(“BooKSTore”);
tf = word.equalsIgnoreCase(“mouse”);
What is the difference between equals and
equalsIgnoreCase methods?
int length()
int len;
len = word.length();
String replace (char oldChar, char newChar)
String newWord;
newWord = word.replace (‘b’, ‘t’);
newWord = word.replace (‘o’, ‘a’);
Again, Strings are immutable, so word is
unchanged through this process. A new
String is created.
String substring (int offset, int endIndex)
newWord = word.substring (1,4);
offset is our starting index, and we go to
endIndex – 1.
length = endIndex - offset
0
1
2
3
4
5
6
7
8
b
o
o
k
s
t
o
r
e
String toLowerCase()
word = “Ms. Carol L Cox”;
word = word.toLowerCase();
String toUpperCase()
word = word.toUpperCase();
String substring (int from)
word = word.substring (8);

substring starting at from and extending
through length() – 1
int indexOf(String s)
int index;
index = word.indexOf (“MS”);
index = word.indexOf (“the”);
//returns -1 because “the” is not in word.
index = word.indexOf (“COX”);