Intro to Java Web-Notes_Part53

Intro to Java Web-Notes_Part53 - In this example the...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: In this example the variable str is assigned a reference to the String "rock". The str variable is then changed so that it refers to the String "sky". The change from "rock" to "sky" changes the str reference variable, not either of the objects "rock" or "sky": String str; str "rock"; str "sky"; Visualization: click here for a Flash animation of the above code . In this animation the red "X" drawn on the "rock" object indicates that the Java garbage collector will eventual collect the unreferenced "rock" object (in Java an unreferenced object is useless and eventually disposed of by a mechanism known as "garbage collection"). String Methods That Yield New Strings The String methods to UpperCase, toLowerCase, substring, replace and trim yield new String objects. For example, in the code below the first instruction creates a String object containing the word "tea" and a reference to that object: String $1 = "tea"; String $2 = sl.toUpperCase(); In the second line of code, the to UpperCase method inside the "tea" String object creates a new String object -- this new object is just like the "tea" object except the characters are uppercase. The $2 variable saves the reference to this newly created object: String Similarly, the replace method will replace all occurences of one character in a string with another character. For example, all occurences of 'e’ are set to 'A' in the newly created String object: String word = "forever"; String Changed = word.replace('e', 'A'); System.out.println(word); System.out.println(changed); The output for the above code is: forever forAvAr The substring method pulls out a portion of an existing String. For example, the following code pulls the word "ran' out of the string "orange": String s = "orange"; String out = s.substring(l, 4); The bounds of the substring are specified by indices. Each letter in a String has an index, as shown below for the String orange. The substring method specifies the index of the first letter of the substring, and the index of the letter just past the end of the desired substring. In our example substringfl, 4) indicated that we wanted the letters from index 1 up to, but not including, index 4. In the figure below, the indices of the characters are indicated in red numbers below the characters: The trim method removes any whitespace at the start or end of a string (whitespace refers to blanks, tabs and newline characters). Therefore, if we had the string str: String str = " and "; Then the following call to the trim method String s2 : str.trim(); would yield the new string "and" (with the blanks removed from the start and end of the string). Programmers commonly re-use one reference variable to keep track of a succession of String objects. For example, if str is referencing the String "freeze": String str : "freeze"; and we want to pull the first four letters out of that word, a common technique is to have str drop its reference to "freeze" and have it reference the newly create string "free": str : str.substring(0,4); ...
View Full Document

Page1 / 2

Intro to Java Web-Notes_Part53 - In this example the...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online