Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 String Class

2 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 where the object is located in memory. A reference lets us know where the object is located in memory. reference Object

3 Declaring an object String name; Class variable In Memory: name

4 Creating an Object name = new String (“Louise”); new className actual new className actual operator parameter “Louise” is a String Literal “Louise” is a String Literal This is instantiation of an object. This is instantiation of an object. In memory: name “Louise” String Object

5 name is an instance of the String Class 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. 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. 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. 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 After an object is instantiated we use the dot operator to access its methods

6 char charAt(int index) Return methodformal type nameparameter char c; c = name.charAt(3); 012345 Louise indexes

7 indexes Indexes always start at 0 and go to length() – 1. Indexes always start at 0 and go to length() – 1. Arrays, ArrayList, and 2D ArrayLists are labeled the same way. Arrays, ArrayList, and 2D ArrayLists are labeled the same way. So, for String name = “Louise”;, what is the index of L? e? So, for String name = “Louise”;, what is the index of L? e? How can we find out what the first character of name is? How can we find out what the first character of name is?

8 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 2 nd 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.

9 int compareTo (String str) return method Formal type nameparameter 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.

10 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!! 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. We can create Strings without using new. String name = “Louise”; Whenever a String literal appears, Java creates a String object. Whenever a String literal appears, Java creates a String object.

11 String concat (String str) What is the result: String word = new String (“book”); word = word.concat (“store”); We could also do… word = word + “store”; word = word + “store”;Or word += “store”; 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. word book 1st bookstore 2nd

12 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?

13 boolean equalsIgnoreCase(String str) boolean tf; tf = word.equalsIgnoreCase(“BooKSTore”); tf = word.equalsIgnoreCase(“mouse”); What is the difference between equals and equalsIgnoreCase methods?

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

15 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.

16 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 012345678 bookstore

17 String toLowerCase() word = “Ms. Carol L Cox”; word = word.toLowerCase();

18 String toUpperCase() word = word.toUpperCase();

19 String substring (int from) word = word.substring (8); substring starting at from and extending through length() – 1 substring starting at from and extending through length() – 1

20 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”);


Download ppt "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."

Similar presentations


Ads by Google