chapter 5 Flashcards
What is string concatentation?
Placing one string before the other and combining them.
What are the two ways the ways the + can be interpreted?
- If both operands are numeric, + means numeric addition.
2. If either operand is a String, + means concatenation.
What is an immutable object?
An object that cannot be changed once it is created.
Is the String type immutable?
yes.
What does String.length() return?
The number of characters in String.
What does String.charAt(1) return?
The char value at index 1 or throws IndexOutOfBoundsException - if the index argument is negative or not less than the length of this string.
What does String.indexOf() do?
Returns the index within this string of the first occurrence of the specified value, or -1.
What are the method signatures for indexOf()?
int indexOf(int ch) int indexOf(int ch, int fromIndex) int indexOf(String str) int indexOf(String str, int fromIndex)
What does the String.substring(int beginIndex) do?
Returns a new string that is a substring of this string. `
What are the method signatures for substring()?
String substring(int beginIndex) String substring(int beginIndex, int endIndex)
Is the value at the endIndex in the substring() included ?
No.
What does toLowerCase() and toUpperCase() do?
toLowerCase(): converts any uppercase characters to lowercase in the returned string.
toUpperCase(): converts any lowercase characters to uppercase in the returned string.
What does the String.equals() method do?
Returns true if the given object represents a String equivalent to this string, false otherwise.
What does the String.equalsIgnoreCase() do?
Returns true if the argument is not null and it represents an equivalent String ignoring case; false otherwise.
What does the startsWith() do?
boolean startsWith(String prefix): Returns true if the character sequence represented by the argument is a prefix of the character sequence represented by this string; false otherwise. Note also that true will be returned if the argument is an empty string or is equal to this String object as determined by the equals(Object) method.
What does the endsWith() do?
boolean endsWith(String suffix): Returns true if the character sequence represented by the argument is a suffix of the character sequence represented by this object; false otherwise. Note that the result will be true if the argument is the empty string or is equal to this String object as determined by the equals(Object) method.
What does the String.replace() do?
Replaces some value(s) with different value(s).
What are the two method signatures for replace()?
replace(char oldChar, char newChar)
replace(CharSequence target, CharSequence replacement)
What does the String.contains() do?
Returns true if and only if this string contains the specified sequence of char values.
What do the trim(), strip(), stripLeading(), and stripTrailing() methods do?
Remove whitespace from the beginning, ending or both ends of the string.
What is the difference between strip() and trim()?
strip() supports Unicode.
What does the String.intern() do?
Returns a string that has the same contents as this string, but is guaranteed to be from a pool of unique strings.
What is the StringBuilder class?
A class to create mutable strings.
What are four common StringBuilder methods?
charAt(), indexOf(), length(), and substring().
What does the StringBuilder.append() do?
Appends the string representation of the argument to this sequence and returns a reference to this StringBuilder.
What does the StringBuilder.insert() do?
Adds characters to the StringBuilder as the requested index and returns a reference to this StringBuilder.
What does StringBuilder.delete(int start, int end) do?
Removes the characters in a substring of this sequence. The substring begins at the specified start and extends to the character at index end - 1 or to the end of the sequence if no such character exists. If start is equal to end, no changes are made. Throws StringIndexOutOfBoundsException - if start is negative, greater than length(), or greater than end.
What does StringBuilder.deleteCharAt(int index) do?
Removes the char at the specified position in this sequence. Throws StringIndexOutOfBoundsException - if the index is negative or greater than or equal to length().
What does StringBuilder.replace(int start,
int end,
String str) do?
Replaces the characters in a substring of this sequence with characters in the specified String. The substring begins at the specified start and extends to the character at index end - 1 or to the end of the sequence if no such character exists. First the characters in the substring are removed and then the specified String is inserted at start. Throws StringIndexOutOfBoundsException - if start is negative, greater than length(), or greater than end.
What does StringBuilder.reverse() do?
Causes this character sequence to be replaced by the reverse of the sequence and returns a reference to this object.
What type of compilation error is thrown if you try to compare two different object types using ==?
incompatible types error.
What is the string pool in Java?
The Java string constant pool is an area in heap memory where Java stores literal string values. The heap is an area of memory used for run-time operations. However, if you create a new instance of a String object, it will create a new string literal in the pool, even if one already exists.
What does the String.intern() do?
Returns a string that has the same contents as this string, but is guaranteed to be from a pool of unique strings.
What is the basic structure of an array?
int[] number = new int[3];
int = type of array
[] Array symbol (required)
3 = size of the array
What is an example of an anonymous array?
int[] numbers = {1,2,3};
Its anonymous because the type and size were not specified.
Where can the [] go when defining an array?
Before or after the name.
What types of variables result from:
int ids[], types;
A variable named ids of type int[]
A variable named types of type int
What method does Java have to print arrays nicely?
Arrays.toString(someArray);