APIs Flashcards
Does calling a method change a String?
ex.: “1234”.replace(‘1’, ‘9’);
No.
Strings are immutable. Calling methods does not change the object, it creates a new String object.
Can you extend String class?
No.
Not because it is immutable, but because it is final.
Can StringBuffer or StringBuilder be extended?
No.
They are final classes.
Can Wrapper classes be extended?
No.
They are final classes.
Does the String class have a method called reverse?
No.
But StringBuilder and StringBuffer do.
How do you represent just a date with no time zone information?
java.time.LocalDate
What are common classes of the java.time package?
LocalDate, LocalTime, LocalDateTime, Instant, Period, Duration are part of this package.
How do you get time zone information?
Use ZonedDateTime
How do you format Date objects?
java.time.format.DateTimeFormatter
LocalDate d1 = LocalDate.parse(“2015-01-01”, DateTimeFormatter.ISO_LOCAL_DATE);
If using ArrayList API, what happens if the fromIndex and toIndex arguments of a subList ( ) method are equal?
The subList returns an empty list.
List foo - new ArrayList( ); foo.add("f"); foo.add("o"); foo.add("o"); List bar = new ArrayList( foo.subList(1, 1) );
T/F: Literal strings within the same class in the same package represent references to the same String object.
True.
T/F: Literal strings within different classes in the same package represent references to the same String object.
True.
When do Strings that are computed by constant expressions actually get computed?
At compile time.
How are Strings computed by constant expressions treated?
When computed at compile time, they are treated as if they were literals.
What does the java string intern( ) method do?
It returns the interned (canonical representation) of string.
What is the result of explicitly interning a computed string?
It is the same string as any pre-existing literal string with the same contents.
T/F: Strings computed at run time are the same as any pre-existing String object with the same contents.
False.
Strings computed at run time are newly created and therefore are distinct.
What will the following code print?
String abc = ""; abc.concat("abc"); abc.concat("def"); System.out.print(abc);
It will print an empty string.
For the String class, what does the following return?
String substring(int beginIndex)
This returns a new string that is a substring of this string.
For the String class, what does the following return?
String substring(int beginIndex, int endIndex)
Returns a new string that is a substring of this string.
For the String class, what does the following return?
int indexOf(int ch)
Returns the index within this string of the first occurrence of the specified character.
For the String class, what does the following return?
int indexOf(int ch, int fromIndex)
Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.
For the String class, what does the following return?
int indexOf(String str, int fromIndex)
Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
For the String class, what does the following return?
int indexOf(String str)
Returns the index within this string of the first occurrence of the specified substring. int indexOf(String str, int fromIndex)
Can String be extended?
No.
It is final.
Does StringBuilder extend String?
No, it extends Object.
What does the following code return?
“hamburger”.substring(4, 8);
urge
What does the following code return?
“smiles”.substring(1, 5)
mile
What does the following code return?
“unhappy”.substring(2)
happy
What does the following code return?
“emptiness”.substring(9)
”” (an empty string)
What does the following code return?
“Harbison”.substring(3)
bison
What will the third line print?
String s = “fancy-“;
s.append(“pants”);
System.out.println(s);
It won’t compile.
There is no append( )
method for String.
Is the following valid syntax?
String String = “String”;
Oddly enough, yes.
There are no restrictions on naming variables the same as classes.