String Immutability n Strings are constants String s
String Immutability n Strings are constants String s = " Tea "; s = s. trim(); s = s. replace('T', 'S'); s 1 " Tea " n 2 "Tea" A string reference may be set: String s = "Tea"; s = "Sea"; 3 "Sea" s 1 "Tea" 2 "Sea"2
String Interning n Avoids duplicate strings String[] array = new String[1000]; for (int i = 0; i < array. length; i++) { array[i] = "Hello world"; } array 1 2 "Hello world" 1000 An immutable string. Thus, can be shared. 3
String Interning (cont. ) n All string literals and string-valued constant expressions are interned. "Hello" string literals "World" String Pool "Hello" + "World" string-valued constant expression 4
5
6
String Constructors n Use implicit constructor: String s = "Hello"; (string literals are interned) Instead of: String s = new String("Hello"); (causes extra memory allocation) 7
The String. Builder Class Represents a mutable character string n Main methods: append() & insert() n accept data of any type • If: sb = new String. Builder("123") Then: sb. append(4) • is equivalent to sb. insert(sb. length(), 4) Both yield "1234" sb “ 1234" “ 123" 8
The Concatenation Operator (+) n String conversion and concatenation: "Hello " + "World" is "Hello World" • "19" + 8 + 9 is "1989" • n Concatenation by String. Builder ¨ int i = 8; String x = "19" + i + 9; is compiled to the equivalent of: String x = new String. Builder(). append("19"). append(8). append(9). to. String(); 9
String. Builder vs. String n Inefficient version using String public static String duplicate(String s, int times) { String result = s; for (int i = 1; i < times; i++) { result = result + s; } return result; A new } String object is created each time 10
String. Builder vs. String (cont. ) n More efficient version with String. Builder: public static String duplicate(String s, int times) { String. Builder result = new String. Builder(s); for (int i = 1; i < times; i++) { result. append(s); } return result. to. String(); no new } Objects 11
String. Builder vs. String (cont. ) n Even more efficient version: public static String duplicate(String s, int times) { String. Builder result = new String. Builder(s. length() * times); for (int i = 0; i < times; i++) { result. append(s); } return result. to. String(); } created with the correct capacity 12
String. Builder vs. String. Buffer n String. Builder has the same API as String. Buffer, but with no guarantee of synchronization. n String. Builder is a replacement for String. Buffer when there is only a single thread n Where possible, it is recommended to use String. Builder as it will be faster under most implementations. 13
: הספר על מבוסס Test-Driven Development By Example By Kent Beck Publisher: Addison Wesley Date: November 08, 2002 ISBN: 0 -321 -14653 -0 Pages: 240
העבודה שלבי 1. 2. 3. 4. 5. Quickly add a test. Run all tests and see the new one fail. Make a little change. Run all tests and see them all succeed. Refactor to remove duplication. 18
JUnit Anotations n @Test @Before , @After n @Before. Class , @After. Class n @Ignore n @Test(timeout=100) n 20
בדיקה עוד נוסיף : חדשה בדיקה מתודת עוד להוסיף אפשר n public void test. Fibonacci. Of. One. Is. One() { assert. Equals(1, fib(1)); } : ב נסתפק אנחנו n אדום . . . נריץ n public void test. Fibonacci() { assert. Equals(0, fib(0)); assert. Equals(1, fib(1)); } TODO List: fib(0) == 0 fib(1) == 1 22
שכתוב : קיבלנו n fib(n-1) בעצם הוא הראשון 1 - ה n fib(n-2) בעצם הוא השני 1 - ה n int fib(int n) { if (n == 0) return 0; if (n <= 2) return 1; return 1 + 1; } int fib(int n) { if (n == 0) return 0; if (n <= 2) return 1; return fib(n-1) + 1; } 28
שכתוב fib(n-2) בעצם הוא השני 1 - ה n וסיימנו , fib(2) עבור נכליל n int fib(int n) { if (n == 0) return 0; if (n <= 2) return 1; return fib(n-1) + fib(n-2); } int fib(int n) { if (n == 0) return 0; if (n == 1) return 1; return fib(n-1) + fib(n-2); } 29
סיכום n n n Made a list of the tests we knew we needed to have working Told a story with a snippet of code about how we wanted to view one operation Made the test compile with stubs Made the test run by committing horrible sins Gradually generalized the working code, replacing constants with variables Added items to our to-do list rather than addressing them all at once 30
יותר כללי בהקשר Write a test. n Make it run n Make it right n 31
Write a test n Think about how you would like the operation in your mind to appear in your code. n You are writing a story. Invent the interface you wish you had. n Include all of the elements in the story that you imagine will be necessary to calculate the right answers. 32
Make it run n n n Quickly getting that bar to go to green dominates everything else If a clean, simple solution is obvious, then type it in If the clean, simple solution is obvious but it will take you a minute, then make a note of it and get back to the main problem, which is getting the bar green in seconds This shift in aesthetics is hard for some experienced software engineers They only know how to follow the rules of good engineering Quick green excuses all sins. But only for a moment 33
Make it right n Now that the system is behaving, put the sinful ways of the recent past behind you n Step back onto the straight and narrow path of software righteousness n Remove the duplication that you have introduced, and get to green quickly 34
- Slides: 35