Chapter 5 Flashcards
Which are immutable: String, StringBuilder, StringBuffer, arrays, or ArrayList?
String is the only immutable class in the list.
What are two ways to create a String object?
String name = "Fluffy"; String name = new String("Fluffy");
What interfaces does String implement?
Serializable, CharSequence, Comparable
What package is String part of?
java.lang
What are the rules for concatenation?
- If both operands are numeric, + means numeric addition.
- If either operand is a String, + means concatenation.
- The expression is evaluated from left to right.
Name as many important String methods as you can (answer contains only methods relevant for OCP exam)
- length()
- charAt()
- indexOf()
- substring()
- toLowerCase() & toUpperCase()
- equals() & equalsIgnoreCase()
- startsWith() & endsWith()
- replace()
- contains()
- trim() & strip() & stripLeading(), and stripTailing()
- intern()
What does the String method length() do?
returns the length of the string, where the first number is 1.
What does the String method charAt() do?
The method charAt() lets you query the string to find out what character is at a specific index.
What does the String method indexOf() do?
The method indexOf() looks at the characters in the string and finds the first index that matches the value.
What does String.indexOf() return?
ex: “test”.indexOf(“z”);
The method returns -1.
What does String.indexOf() return?
ex: “test”.indexOf(“es”);
1
What does String.indexOf() return?
ex: “animals”.indexOf(‘a’);
0.
note: characters can be passed as an argument.
What can be passed as a second argument of the String method indexOf()?
An index that indicates where to start the search.
ex: “animals”.indexOf(‘a’, 4); // returns 4
What will happen when this code is executed?
“test”.charAt(4);
It throws a StringIndexOutOfBoundsException.
What will happen when this code is executed?
“animals”.indexOf(“al”, 5);
It returns -1 (cannot find a match)
What does the String method substring() do?
The method substring() looks for characters in a string and returns part of the string. The first parameter is the index to start with for the returned string.
The String method substring takes two arguments. Is the second argument inclusive in the search?
No.
ex: “animals”.substring(3, 4); // returns m
What will happen when this code is executed?
“animals”.substring(3, 3);
Returns an empty string
What will happen when this code is executed?
“animals”.substring(3, 2);
or
“animals”.substring(3, 8);
Throws an exception.
is the String method equals() case sensitive?
yes
To use equals without case sensitivity, use the method equalsIgnoreCase().
What will happen when this code is executed?
“abc”.equals(“ABC”);
It returns false. Equals is case sensitive.
What will happen when this code is executed?
“abcabc”.replace(‘a’, ‘A’);
It returns “AbcAbc”
What do the strip() and trim() string methods do?
They remove whitespace from the beginning and end of a string.
Whitespace includes \t (tab, \n (newline) and \r (carriage return).
What do the string methods stripLeading() and stripTrailing() do?
stripLeading() removes whitespace from the beginning and stripTrailing() removes whitespace at the end of a string.
Whitespace includes \t (tab, \n (newline) and \r (carriage return).
What does the string method intern() do?
The intern() method returns the value from the string pool if it is there. Otherwise, it adds the value to the string pool.
What is the following technique called?
(bonus: how many objects are created?)
String result = “AniMal “.trim().toLowerCase().replace(‘a’, ‘A’);
The technique is called method chaining.
Bonus answer: 4 objects
Why should we use Stringbuilder instead of String?
Stringbuilder is mutable, whereas Strings are immutable. If we need to work on a piece of String, it might be better to use a StringBuilder to avoid the creation of many objects.
What are the options to construct a StringBuilder?
Stringbuilder b1 = new StringBuilder(); Stringbuilder b2 = new StringBuilder("animal"); Stringbuilder b3 = new StringBuilder(10);
What is the difference between StringBuilder and StringBuffer?
StringBuffer is thread-safe. StringBuilder is faster.
What is an easy way to reverse a String?
Convert it to a StringBuilder and call the reverse() method.