Ch3: Core Java APIs Flashcards
(89 cards)
What does API stand for?
Application Programming Interface
What is the value of s1 and s2, and why?
String s1 = “1”;
String s2 = s1.concat(“2”);
s2.concat(“3”);
s1 = “1”
s2 = “12”
The String object is immutable, so the .concat method doesn’t modify the object.
What’s another name for the string pool?
The intern pool
What’s another name for the intern pool?
The string pool
What goes into the string pool?
String literals (not String objects created with “new”)
How do you get the number of characters in a String?
.length()
How do you find out what is at a specific index in a string?
.charAt(int index)
How do you find the index of a specific item in a string?
indexOf(char ch) or indexOf(String str)
How do you find the index of a specific item in a string starting from a specific position?
indexOf(char ch, index int)
What happens when you call indexOf for a value that does not exist?
The value -1 is returned
How would you return the value “123” from the object:
String str = “012345”;
str.substring(1,4) // value at the ending index will not be included
What is the method signature to replace characters?
str.replace(orig, new)
How do you remove trailing and leading whitespace from a string and what is removed?
.trim() removes space, new line (\n), tab (\t), and carriage return (\r)
What does the .append() method on StringBuilder return?
A reference to itself
List all StringBuilder constructor signatures.
new StringBuilder(); // empty object new StringBuilder("foo"); // contains val "foo" new StringBuilder(10); // Empty, but capacity of 10
What are the two StringBuilder methods for adding text to the string in the object?
.append(String str)
.insert(int index, String str)
What are the two StringBuilder methods for removing text from the string in the object?
.delete(int startIndex, int stopIndex) // stopIndex is not removed, same as substring
.deleteChatAt(int index)
Which StringBuilder method is used to reorder the text backwards?
.reverse()
What is StringBuffer?
The same as StringBuilder, but slower because it’s thread safe.
What does the StringBuilder.equals(StringBuilder obj) test?
Checks for reference equality (not value equality)
for
char[] letters;
what kind of variable is letters?
A reference variable pointing to an array of primitive char types
What kind of data structure is an array?
Ordered list
How do you create an empty array of ints?
int[] numbers = new int[3];
How do you create an array of ints with values? (2 ways)
int[] numbers = new int[] {11, 20, 45};
int[] numbers = {11, 20, 45};