string class. variable holds a primitive value or a reference to an object. holds a primitive value...

20
String Class String Class

Upload: drusilla-parsons

Post on 17-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know

String ClassString Class

Page 2: String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know

VariableVariable

Holds a primitive value or a reference to Holds a primitive value or a reference to an object.an object.

A reference lets us know where the object A reference lets us know where the object is located in memory.is located in memory.

referenceObject

Page 3: String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know

Declaring an objectDeclaring an object

String name;String name;Class variableClass variable

In Memory:In Memory:name

Page 4: String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know

Creating an ObjectCreating an Object

name = new String (“Louise”);name = new String (“Louise”); new new className actual className actual

operator operator parameterparameter

““Louise” is a Louise” is a String LiteralString LiteralThis is This is instantiation of an object.instantiation of an object.

In memory:In memory:name “Louise”

String Object

Page 5: String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know

name is an name is an instance instance of the String Class of the String Classnewnew operator calls a operator calls a constructor constructor which is which is

a method that has the same name as the a method that has the same name as the class.class.

Constructor: Constructor: used to set the field data of used to set the field data of an object. Gives its initial set up.an object. Gives its initial set up.

In this example, the constructor is passed In this example, the constructor is passed a string literal that specifies the characters a string literal that specifies the characters that the String object will hold.that the String object will hold.

After an object is instantiated we use the After an object is instantiated we use the dot operatordot operator to access its methods to access its methods

Page 6: String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know

char charAt(int index)char charAt(int index)Return Return methodmethod formalformal

type type namename parameterparameter

char c;char c;

c = name.charAt(3);c = name.charAt(3);

00 11 22 33 44 55

LL oo uu ii ss ee

indexes

Page 7: String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know

indexesindexes

Indexes always start at 0 and go to Indexes always start at 0 and go to length() – 1.length() – 1.

Arrays, ArrayList, and 2D ArrayLists are Arrays, ArrayList, and 2D ArrayLists are labeled the same way.labeled the same way.

So, for String name = “Louise”;, what is So, for String name = “Louise”;, what is the index of L? e?the index of L? e?

How can we find out what the first How can we find out what the first character of name is?character of name is?

Page 8: String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know

ExerciseExercise

Write a series of statements which declares Write a series of statements which declares and creates a String which will hold your and creates a String which will hold your school name and a char variable to hold school name and a char variable to hold the 2the 2ndnd letter in the school name. 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.

Page 9: String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know

int compareTo (String str)int compareTo (String str)

returnreturn method method Formal Formal typetype name name parameterparameter

In the interactions pane declare and create a In the interactions pane declare and create a String named word that is assigned to the String String named word that is assigned to the String “book”. Use compareTo to compare it with “book”. Use compareTo to compare it with “took”, “Book”, and “bark”“took”, “Book”, and “bark”

If the string comes before our word alphabetically If the string comes before our word alphabetically we get a positive return value, if it comes after we get a positive return value, if it comes after our word, we get a negative. If the words are our word, we get a negative. If the words are exactly the same, we get 0. exactly the same, we get 0.

Uppercase comes before lowercase in unicodes.Uppercase comes before lowercase in unicodes.

Page 10: String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know

Short Cut to String CreationShort Cut to String Creation

Because the String class is used so often, Because the String class is used so often, Java allows it to act like a primitive data. Java allows it to act like a primitive data. But ALWAYS REMEMBER IT IS AN But ALWAYS REMEMBER IT IS AN OBJECT!!OBJECT!!

We can create Strings without using We can create Strings without using newnew..String name = “Louise”;String name = “Louise”;

Whenever a String literal appears, Java Whenever a String literal appears, Java creates a String object.creates a String object.

Page 11: String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know

String concat (String str)String concat (String str)

What is the result:What is the result:

String word = new String (“book”);String word = new String (“book”);word = word.concat (“store”);word = word.concat (“store”);

We could also do…We could also do… word = word + “store”;word = word + “store”;OrOr

word += “store”;word += “store”;

Remember Strings are immutable – they can not be changed, so when we Remember Strings are immutable – they can not be changed, so when we concatenate book and store, we are actually creating a new String object concatenate book and store, we are actually creating a new String object “bookstore” and having word reference it instead. Since the original String “bookstore” and having word reference it instead. Since the original String “book” has “book” has nono reference, Java does automatic garbage collection and reference, Java does automatic garbage collection and removes it from memory.removes it from memory.

word

book

1st

bookstore2nd

Page 12: String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know

boolean equals (String str)boolean equals (String str)

boolean tf;boolean tf;

tf = word.equals (“BOOKSTORE”);tf = word.equals (“BOOKSTORE”);

tf = word.equals (“bookstore”);tf = word.equals (“bookstore”);

Try these in DrJava’s interaction pane, and Try these in DrJava’s interaction pane, and record the results. What does equals do?record the results. What does equals do?

Page 13: String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know

boolean equalsIgnoreCase(String str)boolean equalsIgnoreCase(String str)

boolean tf;boolean tf;

tf = word.equalsIgnoreCase(“BooKSTore”);tf = word.equalsIgnoreCase(“BooKSTore”);

tf = word.equalsIgnoreCase(“mouse”);tf = word.equalsIgnoreCase(“mouse”);

What is the difference between equals and What is the difference between equals and equalsIgnoreCase methods?equalsIgnoreCase methods?

Page 14: String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know

int length()int length()

int len;int len;

len = word.length();len = word.length();

Page 15: String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know

String replace (char oldChar, char newChar)String replace (char oldChar, char newChar)

String newWord;String newWord;

newWord = word.replace (‘b’, ‘t’);newWord = word.replace (‘b’, ‘t’);

newWord = word.replace (‘o’, ‘a’);newWord = word.replace (‘o’, ‘a’);

Again, Strings are immutable, so word is Again, Strings are immutable, so word is unchanged through this process. A new unchanged through this process. A new String is created.String is created.

Page 16: String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know

String substring (int offset, int endIndex)String substring (int offset, int endIndex)

newWord = word.substring (1,4);newWord = word.substring (1,4);

offset is our starting index, and we go to offset is our starting index, and we go to endIndex – 1.endIndex – 1.

length = endIndex - offsetlength = endIndex - offset

00 11 22 33 44 55 66 77 88

bb oo oo kk ss tt oo rr ee

Page 17: String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know

String toLowerCase()String toLowerCase()

word = “Ms. Carol L Cox”;word = “Ms. Carol L Cox”;

word = word.toLowerCase();word = word.toLowerCase();

Page 18: String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know

String toUpperCase()String toUpperCase()

word = word.toUpperCase();word = word.toUpperCase();

Page 19: String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know

String substring (int from)String substring (int from)

word = word.substring (8);word = word.substring (8);

substring starting at from and extending substring starting at from and extending through length() – 1 through length() – 1

Page 20: String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know

int indexOf(String s)int indexOf(String s)

int index;int index;

index = word.indexOf (“MS”);index = word.indexOf (“MS”);

index = word.indexOf (“the”);index = word.indexOf (“the”);

//returns -1 because “the” is not in word.//returns -1 because “the” is not in word.

index = word.indexOf (“COX”);index = word.indexOf (“COX”);